![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
I recently posted a question that was about obtaining the variables in the URL. I received a few great answers and am using the code that was provided to me, however, seeing as I am still pretty new to javascript, I was wondering if someone could help explain some of the commands in this function. In addition to getting things to work, I also like to have a full understanding of what is going on. Here is the code: var x={}; // global variable referencing empty object function loadURLVariables() { var searchPairs = location.search.substring(1).split("&"); for (var i in searchPairs) { var pair = searchPairs[i].split("="); x[pair[0]]=unescape(pair[1].replace(/\+/g," ")); /* if this is the result of a form action */ } } 1) var x={} - I think this creates a new object that is empty. 2) for (var i in searchPairs) - I haven't seen this notation before, in the past I have used the typical i=0; i < searchPairs.length; i++. Is this just short hand notation? 3) x[pair[0]]=unescape(pair[1].replace(/\+/g," ")); - This is the line that really throws me. So we are creating an array in which the key is pair[0], which allows me to call the variables by x.foo or x[foo]. I have no idea what the whole unescape and regular expression is for. Thanks for your help, Andrew V. Romero |
#2
| |||
| |||
|
|
1) var x = {}; // I think this creates a new object that is empty. 1. This creates a new array with nothing in it. I wouldn't do it this way personally. I would do var x = new Array(); or to be more precise I would probably call it arrQueryString or something like that. |
![]() |
| Thread Tools | |
| Display Modes | |
| |