weiyee08
asked on
Get QueryString
How do I use JavaScript to get QueryString ?
queryString = window.location.href.subst ring(windo w.location .href.inde xOf('?') + 1);
:o)
Ant
:o)
Ant
Ant , why parse it when you can use the location Object propertie ?
Hi,
The query string is held in the browsers location object.
The search property of the location object stores the query string, this is a read/write string property so you can both get and set the query string.
access it like this
somestring = location.search;
hope this helps
Gordon McMullan
The query string is held in the browsers location object.
The search property of the location object stores the query string, this is a read/write string property so you can both get and set the query string.
access it like this
somestring = location.search;
hope this helps
Gordon McMullan
gordonmcmullan ,
Welcome to EE.
Please read :
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20170202#tip
Welcome to EE.
Please read :
https://www.experts-exchange.com/jsp/qShow.jsp?qid=20170202#tip
ASKER
but how do I separate the fields ?
ASKER
a.marsh's answered is much better
Try this :
query=self.location.search ;
valuesArr=query.split("&") ;
query=self.location.search
valuesArr=query.split("&")
valuesArr will now hold an Array() of the name value pairs.
I am using the location object! With my way you also never get the ? at the start.
Ant
Ant
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
a.marsh,
var tempArray = array1[i].split('=');
eval(tempArray[0] + " = \"" + tempArray[1] + "\"");
what do above statement mean ?
document.write(your_name);
why you don't use array to display ?
var tempArray = array1[i].split('=');
eval(tempArray[0] + " = \"" + tempArray[1] + "\"");
what do above statement mean ?
document.write(your_name);
why you don't use array to display ?
Basically:
var queryString = window.location.href.subst ring((wind ow.locatio n.href.ind exOf('?') + 1));
var array1 = queryString.split('&'); // Each array element is in format "fieldname=value"
gets the query string and generates an array. The values of the array are:
fieldname=value
So in order to separate the value from the field we do a further split hence the:
var tempArray = array1[i].split('=');
This gives us an array that looks like:
tempArray[0] = "fieldname";
tempArray[1] = "value";
If you create the two files and copy and paste the code in the example above you will see it working.
:o)
Ant
var queryString = window.location.href.subst
var array1 = queryString.split('&'); // Each array element is in format "fieldname=value"
gets the query string and generates an array. The values of the array are:
fieldname=value
So in order to separate the value from the field we do a further split hence the:
var tempArray = array1[i].split('=');
This gives us an array that looks like:
tempArray[0] = "fieldname";
tempArray[1] = "value";
If you create the two files and copy and paste the code in the example above you will see it working.
:o)
Ant
ASKER
i kow it is working, but why you don't use array to display the name ?
weiyee08 ,
I would change the code to use Hash-Table instead of Array.
I would change the code to use Hash-Table instead of Array.
The name is simply stored in a variable so it can be called easily time and time again.
You could quite easily store all the names in an array.
There is always more than one way of doing the same thing!
:o)
Ant
You could quite easily store all the names in an array.
There is always more than one way of doing the same thing!
:o)
Ant
ASKER
a.marsh,
eval(tempArray[0] + " = \"" + tempArray[1] + "\"");
above statement will create a variable and store the value in it, right ?
avner,
what do you mean by Hash-Table ? How to do it ? Can you show me ?
Which one is the best ?
eval(tempArray[0] + " = \"" + tempArray[1] + "\"");
above statement will create a variable and store the value in it, right ?
avner,
what do you mean by Hash-Table ? How to do it ? Can you show me ?
Which one is the best ?
Yes - the eval() function basically executes the string as code. So for example the first call of the loop actually does this:
eval("your_name = \"Ant\"");
assuming of course I entered the value Ant on the first page.
The eval function executes that code hence setting the variable "your_name" to the value "Ant".
:o)
Ant
eval("your_name = \"Ant\"");
assuming of course I entered the value Ant on the first page.
The eval function executes that code hence setting the variable "your_name" to the value "Ant".
:o)
Ant
I'll let avner answer his part, I don't want to step on anyones' toes - although I think what he is suggesting is more commonly known as "associative arrays"
:o)
Ant
:o)
Ant
ASKER
a.marsh,
I don't mind if you answer his idea as long as you are the one who answer first
I don't mind if you answer his idea as long as you are the one who answer first
Okay, well I don't know if this is exactly what avner had in mind, but this is what I believe he is suggesting:
story.html
-------------------------- ---
<html>
<head>
<title>The Story</title>
<script language="javascript">
nameArray = new Array();
// Get names
var queryString = window.location.href.subst ring((wind ow.locatio n.href.ind exOf('?') + 1));
var array1 = queryString.split('&'); // Each array element is in format "fieldname=value"
for(var i = 0; i < array1.length; i++){
var tempArray = array1[i].split('='); // Separate fieldname and value
eval("nameArray[\"" + tempArray[0] + "\"] = \"" + tempArray[1] + "\"");
}
</script>
</head>
<body>
<p>This is the story of
<script language="javascript"> document.write(nameArray[" your_name" ]);</scrip t>'s amazing adventure with
<script language="javascript"> document.write(nameArray[" friend1"]) ;</script> and
<script language="javascript"> document.write(nameArray[" friend2"]) ;</script> .
</p>
<p>Now I'm not a very good storyteller, so I'm afraid that's all you're going to hear about
<script language="javascript"> document.write(nameArray[" your_name" ]);</scrip t>,
<script language="javascript"> document.write(nameArray[" friend1"]) ;</script> and
<script language="javascript"> document.write(nameArray[" friend2"]) ;</script> 's adventure!
</p>
</form>
</body>
</html>
Ant
story.html
--------------------------
<html>
<head>
<title>The Story</title>
<script language="javascript">
nameArray = new Array();
// Get names
var queryString = window.location.href.subst
var array1 = queryString.split('&'); // Each array element is in format "fieldname=value"
for(var i = 0; i < array1.length; i++){
var tempArray = array1[i].split('='); // Separate fieldname and value
eval("nameArray[\"" + tempArray[0] + "\"] = \"" + tempArray[1] + "\"");
}
</script>
</head>
<body>
<p>This is the story of
<script language="javascript"> document.write(nameArray["
<script language="javascript"> document.write(nameArray["
<script language="javascript"> document.write(nameArray["
</p>
<p>Now I'm not a very good storyteller, so I'm afraid that's all you're going to hear about
<script language="javascript"> document.write(nameArray["
<script language="javascript"> document.write(nameArray["
<script language="javascript"> document.write(nameArray["
</p>
</form>
</body>
</html>
Ant
ASKER
a.marsh
I think you are right. Thanks !
I think you are right. Thanks !
Glad to help.
:o)
Ant
:o)
Ant
alert(self.location.search
</script>