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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts

    Question Just curious - how to do: Name JS variables the same as URL parameters

    Hello, everyone.

    I'm looking for a way to take URL parameters (via document.URL) and name JavaScript variables the same as the URL parameters.

    For example: Let's say the URL is http://www.domain.com/index.html?a=1...ething&c=carpe diem.

    I've got code that will split on "?", then split on "&", then split on "=", so I've got an array. How can I now loop through that array, and set var array[0] = array[1], and get JavaScript variables a, b, and c with their associated value?

    Thank you,

    PS: I should add - WITHOUT using eval. eval() is evil.
    Last edited by WolfShade; 09-04-2012 at 08:09 PM.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Maybe it will help you if you realize that every variable name in the global scope is automatically the key of an associative array called "window" if you use it as a string

    i.e.
    Code:
    var myVar = 5;
    alert(window["myVar"]);  // will give 5
    
    window["myNewVar"] = "Hello";
    alert(myNewVar);   // will give "Hello"
    So you could do it like this
    Code:
    window[array[0]] = array[1];

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    970
    Thanks
    19
    Thanked 212 Times in 210 Posts
    Code:
    <script>
    arr=["dog","fido","cat","puss"]
    var a,b;
    while((a=arr.shift()) && (b=arr.shift())){
    	window[a]=b;
    }
    alert(dog)
    alert(cat)
    </script>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,992
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Code:
    <script type="text/javascript">
    
    var str = "http://www.domain.com/index.html?a=1&b=something&c=carpe"
    str = str.replace(/\?|&|=/gi, "~");
    var strsplit = str.split("~");
    var a = strsplit[2];
    alert (a);
    var b = strsplit[4];
    alert (b);
    var c = strsplit[6];
    alert (c);
    
    </script>
    Code:
    <script type="text/javascript">
    
    var str = "http://www.domain.com/index.html?a=1&b=something&c=carpe"
    str = str.replace(/\?|&|=/gi, "~");
    var strsplit = str.split("~");
    
    for (var i =0; i<strsplit.length; i++) {
    if (strsplit[i] == "a") {
    var a = strsplit[i+1];
    alert ("a = " + a);
    }
    if (strsplit[i] == "b") {
    var b = strsplit[i+1];
    alert ("b= " + b)
    }
    if (strsplit[i] == "c") {
    var c = strsplit[i+1];
    alert ("c= " + c)
    }
    }
    
    </script>

    "Education is the process of casting imitation pearls before real swine" - Irwin Edman

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,335
    Thanks
    11
    Thanked 588 Times in 569 Posts
    code
    Code:
    // turn URL'QS into an object using a parser. takes full urls...
    function parseQS(str) {
    
    	function cast(v){var builtIn=cast.lut[v];return Number(v)||(builtIn!==undefined?builtIn:v);}; 
    		cast.lut=({ 'true':true, 'false':false,"":null});
    		
    		
        var ob = {}, float = "",
            key = "",
            dc = decodeURIComponent;
    
        for (var i = 0, mx = str.length; i < mx; i++) {
            var it = str[i];
            if (it === "=") {
                key = float;
                float = "";
                continue;
            }
            if (!it.search(/^[?&]/)) {
                if (it === "&" && str.slice(i + 1, i + 5) === "amp;") {
                    i = (i + 4);
                    float += "&";
                    continue;
                }
                if (key) {
                    ob[key] = cast(dc(float));
                }
                key = "";
                float = "";
                continue;
            }
            float += it;
        }
        ob[key] = dc(float);
        return ob;
    }
    demo
    Code:
    alert(
     JSON.stringify(
      parseQS(
         "https://www.google.com/search?num=123&bool=false&q=css+cookies+switch+stylesheet&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-beta&channel=fflb"
      )
      , null, "\t"
     )
    )

    shows:
    Code:
    {
    	"num": 123,
    	"bool": false,
    	"q": "css+cookies+switch+stylesheet",
    	"ie": "utf-8",
    	"oe": "utf-8",
    	"aq": "t",
    	"rls": "org.mozilla:en-US:official",
    	"client": "firefox-beta",
    	"channel": "fflb"
    }
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Awesome! Thank you, guys, for the input. I'm going to give each of them a shot and report back.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #7
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    All suggestions were excellent, but devnull69's sample was very simple and nailed it.

    Thanks, again, everyone.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".


  •  

    Posting Permissions

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