CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Just curious - how to do: Name JS variables the same as URL parameters (http://www.codingforums.com/showthread.php?t=272251)

WolfShade 09-04-2012 08:00 PM

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.

devnull69 09-04-2012 08:37 PM

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];

DaveyErwin 09-04-2012 08:42 PM

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>


Philip M 09-04-2012 09:02 PM

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

rnd me 09-05-2012 11:43 AM

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"
}


WolfShade 09-05-2012 01:40 PM

Awesome! Thank you, guys, for the input. I'm going to give each of them a shot and report back.

WolfShade 09-05-2012 04:49 PM

All suggestions were excellent, but devnull69's sample was very simple and nailed it.

Thanks, again, everyone.


All times are GMT +1. The time now is 07:09 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.