Results 1 to 6 of 6

Thread: Parsing a querystring [Resolved]

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  2. #2

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Bah, I'm too accustomed to VB...

    if x=="PI"{

    Should be...

    if(x=="PI"){

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974
    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

  4. #4
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    You do realize how much easier this would be if it were done in PHP?
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    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.

  6. #6

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by ober5861
    You do realize how much easier this would be if it were done in PHP?
    Absolutely. And ASP and ASP.NET as well.

    But I'm having to modify an existing HTML page! Yes, can you believe it, we have an HTML page here!


    Edit: BTW, I didn't try out Acidic's code. I was missing the () in my original post. All's well now, thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width