Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using JS to pull url variables.

    url example:

    whatever.com/index.html?name=username&password=password&gender=male&age=18
    Code:
    function getE(){
      var url_string = String(document.location);
      var url_array1 = url_string.split('?');
      if(url_array1[1]){
      var e_array = url_array1[1].split('&');
      for(i=0;i<=20;i++){
       if(e_array[i]){
        pullE(e_array[i]);
       }
      }
      }
    }   
    function pullE(val){
     var useVal = val.split('=');
     alert(useVal[0]+"  ==  "+useVal[1]); //display purposes
    }
    this would alert in order:
    name == username
    password == password
    gender == male
    age == 18

    where useVal[0] is the element name and useVal[1] is the value.
    Last edited by rlemon; 05-18-2005 at 05:51 PM.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    doesn't have to be two functions. i just like to break it out into two functions so i can compress the top function and still work with the output.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here is it in one function

    Code:
    function getE(){
           	var url_string = String(document.location);
           	var url_array1 = url_string.split('?');
    		if(url_array1[1]){
    			var e_array = url_array1[1].split('&');
    			for(i=0;i<=(e_array.length);i++){
    				if(e_array[i]){
    					var useVal = e_array[i].split('=');
    					alert(useVal[0]+"  ==  "+useVal[1]);
    				}
    			} 
    		}
    }
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #4
    New Coder
    Join Date
    Jun 2002
    Location
    Sydney, NSW, Australia
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's an alternate approach;

    Code:
    function get(name) {
    	var q = unescape(location.search.substring(1)).split(/[=&]/);
    	for (var j=0; j<q.length; j+=2) {
    		if (q[j] == name) {
    			return q[j+1];
    		}
    	}
    	return null;
    }
    Use get("whatever") to retrieve each parameter.
    Last edited by Astro-Boy; 05-20-2005 at 01:17 AM.

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Astro-Boy
    Here's an alternate approach;

    Code:
    function get(name) {
    	var q = location.search.substring(1).split(/[=&]/);
    	for (var j=0; j<q.length; j+=2) {
    		if (q[j] == name) {
    			return q[j+1];
    		}
    	}
    	return null;
    }
    Use get("whatever") to retrieve each parameter.
    i'm not see'n it
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    There are more robust parse querystring scripts here in CF.

    Parse Query String
    Parse search string into get variables

    Your script did not consider important things such as unescaping the name and value pair and replacing +'s with spaces.

  • #7
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    There are more robust parse querystring scripts here in CF.

    Parse Query String
    Parse search string into get variables

    Your script did not consider important things such as unescaping the name and value pair and replacing +'s with spaces.
    I agree it's not the best method of pulling url vars.
    however i specifically wrote it for an application i'm writing, figured other people might find it usefull so... posted it here
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #8
    New Coder
    Join Date
    Jun 2002
    Location
    Sydney, NSW, Australia
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rlemon
    i'm not see'n it
    I'm an idiot and forgot to unescape()

    Code:
    function get(name) {
    	var q = unescape(location.search.substring(1)).split(/[=&]/);
    	for (var j=0; j<q.length; j+=2) {
    		if (q[j] == name) {
    			return q[j+1];
    		}
    	}
    	return null;
    }
    This is what happens when you don't test your code people!

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by john33
    Since a server-side language (ASP) is used here, you should use it to parse the querystring and not use javascript.
    Code:
    <%
    dim user
    user = Request.QueryString("login")
    ...
    %>

  • #10
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no ServerSideLang is being used glenn

    edit: ohh that guys post. gotcha.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #11
    New Coder
    Join Date
    Jun 2005
    Location
    Toronto
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function get(name) {
    	var q = unescape(location.search.substring(1)).split(/[=&]/);
    	for (var j=0; j<q.length; j+=2) {
    		if (q[j] == name) {
    			return q[j+1];
    		}
    	}
    	return null;
    }
    Of course, by "j += 2" you're assuming that the query args would always come in name/value pairs and you wouldn't have single name tokens such as:

    ?name1=value1&orphantoken&name2=value&...

    It might be a better idea to first split by "&", and then by "=", in that order.

    Also, since the function is splitting and scanning the entire query string each time a name is queried, it's a bit inefficient (but, hey, this is an interpreted language anyways -- who cares about that much efficiency )

    While we're at parsing the entire query string and strolling through the tokens in a for loop, we could populate a global associative array so that we can simply access the value we want through something like g_querystring["name1"].

    Code:
    function parseQueryString()
    {
        var obj = new Object();
    
        var nvpairs = location.search.substring(1).split("&");
    
        for (var idx = 0; idx < nvpairs.length; idx++)
        {
            var tokens = nvpairs[idx].split("=");
    
            obj[unescape(tokens[0])] = tokens.length == 2 ?
                unescape(tokens[1]) : undefined;
        }
        
        return obj;
    } 
    
    var g_querystring = parseQueryString();
    
    document.write("a=" + g_querystring["a"] + "<br>");
    document.write("b=" + g_querystring["b"] + "<br>");
    document.write("c=" + g_querystring["c"] + "<br>");
    
    // Iterate through all name/value pairs
    for (var name in g_querystring)
    {
        document.write(name + "=" + g_querystring[name] + "<br>");
    }
    One could even go about calling this global object "Request.QueryString" as an ode to ASP

    Code:
    var Request = new Object();
    Request.QueryString = parseQueryString();
    alert(Request.QueryString["foo"]);
    Last edited by MaGnA; 06-08-2005 at 11:26 PM.

  • #12
    New Coder
    Join Date
    Jun 2005
    Location
    Toronto
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This version handles decoding the "+" as well and hence is the correct way of doing the URL-decoding:

    Code:
    function urlDecode(s)
    {
        return unescape(s.replace("+", " "));
    }
    
    function parseQueryString()
    {
        var obj = new Object();
    
        var nvpairs = location.search.substring(1).split("&");
    
        for (var idx = 0; idx < nvpairs.length; idx++)
        {
            var tokens = nvpairs[idx].split("=");
    
            obj[urlDecode(tokens[0])] = tokens.length == 2 ?
                urlDecode(tokens[1]) : undefined;
        }
        
        return obj;
    }

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I stand by my suggestion of using ASP to parse the querystrings since an .asp page is used.

    But if in case of not using asp (or any server-side language for that matter) and the only choice is javascript, the links I posted in my post #7 are more robust parse querystring scripts as they not only cover decoding of + but also allow multiple values which I think no scripts posted here do.

  • #14
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    I stand by my suggestion of using ASP to parse the querystrings since an .asp page is used.

    But if in case of not using asp (or any server-side language for that matter) and the only choice is javascript, the links I posted in my post #7 are more robust parse querystring scripts as they not only cover decoding of + but also allow multiple values which I think no scripts posted here do.
    hehe
    MINE DOES!! it doesn't decode that stuff tho. i call it a trade off (actually i built it for a specific purpose and never have to decode the values i'm passing but i'm passing multiple values so i know mine works.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #15
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you guys suggest anything for doing this on a local HTML page. To be more specific...I want to do this on a CD. I've tried rlemon single function example, but for some reason it didnt work. In my own troubleshooting I have tried to alert the varialbe containing the enture URL. Anything after the ? doesnt even show up...nor does the ?

    Is this a javascript thing or a browser thing? Can anyonhe help?

    Thanks

    *******

    Nevermind...I believe I solved my own problem. I didn't have file:/// in front of my local path to the page. I was testing with IE and when trying FireFox...it new it was local and put it in for me. When doing it in IE the script worked fine.

    Thanks again.
    Last edited by toddaa; 07-05-2005 at 01:40 PM.


  •  

    Posting Permissions

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