|
-
Apr 22nd, 2004, 08:24 AM
#1
Parsing a querystring [Resolved]
I have a page, which will be called like so:
pagename.html?Referrer=PI
I need to use javascript to parse the querystring, and I simply need to check whether
Referrer = PP
Referrer = PI
or referrer simply isn't there.
This is the code I have, but it doesn't work.
Code:
<script language="javascript">
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();
x = querystring("Referrer");
//This is my main function, "goback", I'm trying to work with x
function goback(){
if x=="PP"{
document.location.href='page1.html';
}
else if x=="PI"{
document.location.href=page2.html';
}
else{
document.location.href=page3.html';
}
}
</script>
I am calling it like this:
Code:
<img src="brain.gif" onClick="javascript:goback();">
I'd appreciate any help. 
Edit: When I say "it doesn't work", it means I'm getting an error, "Object expected". That doesn't give me an idea as to what the problem is.
Last edited by mendhak; Apr 23rd, 2004 at 12:25 AM.
-
Apr 22nd, 2004, 08:39 AM
#2
Bah, I'm too accustomed to VB...
Should be...
-
Apr 22nd, 2004, 08:42 AM
#3
I would recommend changing querystring.keys and querystring.values to something different (eg: querystring_keys and querystring_values ), as they are probably being interpreted as members of the function querystring.
also, page2.html and page3.html should have quotes before and after them
-
Apr 22nd, 2004, 08:46 AM
#4
Frenzied Member
You do realize how much easier this would be if it were done in PHP?
-
Apr 22nd, 2004, 09:09 AM
#5
Frenzied Member
Code:
URL = location.href
temp = URL.lastIndexOf("=")
query_string = URL.substring(parseInt(temp)+1,URL.length)
alert(query_string)
untested but should work.
Have I helped you? Please Rate my posts. 
-
Apr 23rd, 2004, 12:22 AM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|