/    Sign up×
Community /Pin to ProfileBookmark

help getting variables from url

the following two functions came from a javascript forum and work fine. the if loop at the end is my attempt to write different text to the document depending on the variable “entry” in the URL. however, no matter what i do, it will only write the result for the first condition (it writes “entry=1” no matter what entry is). please help! what’s wrong with my if loop?

thanks,

corey

function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.length;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
var query = window.location.search.substring(1);
var pairs = query.split(“&”);

for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf(‘=’);
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryString.keys.length] = argname;
QueryString.values[QueryString.values.length] = value;
}
}

}

QueryString_Parse();

entryID=QueryString(“entry”);

if (entryID=’1′) {
document.write (‘entry=1’);
} else if(entryID=’2′) {
document.write (‘entry=2’);
} else{
document.write (‘no entry specified’);
}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@fredmvApr 06.2004 — [i]Originally posted by secretagentgel [/i]

[B]if (entryID[color=blue]=[/color]='1') {

document.write ('entry=1');

} else if(entryID[color=blue]=[/color]='2') {

document.write ('entry=2');

} else{

document.write ('no entry specified');

} [/B]
[/QUOTE]
Though, you might want to consider using [url=http://www.webdevfaqs.com/javascript.php#querystring]better query string parser[/url].
Copy linkTweet thisAlerts:
@secretagentgelauthorApr 06.2004 — thanks. i now see what my mistake was on my example, but this looks like a better option. more code tho.

corey
×

Success!

Help @secretagentgel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...