Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7644
  • Last Modified:

Get QueryString

How do I use JavaScript to get QueryString ?
0
weiyee08
Asked:
weiyee08
  • 9
  • 7
  • 6
  • +1
1 Solution
 
avnerCommented:
<script>
alert(self.location.search)
</script>
0
 
a.marshCommented:
queryString = window.location.href.substring(window.location.href.indexOf('?') + 1);


:o)

Ant
0
 
avnerCommented:
Ant , why parse it when you can use the location Object propertie ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
gordonmcmullanCommented:
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

0
 
avnerCommented:
gordonmcmullan ,
Welcome to EE.
Please read :
http://www.experts-exchange.com/jsp/qShow.jsp?qid=20170202#tip

0
 
weiyee08Author Commented:
but how do I separate the fields ?
0
 
weiyee08Author Commented:
a.marsh's answered is much better
0
 
avnerCommented:
Try this :

query=self.location.search;
valuesArr=query.split("&");
0
 
avnerCommented:
valuesArr will now hold an Array() of the name value pairs.
0
 
a.marshCommented:
I am using the location object! With my way you also never get the ? at the start.

Ant
0
 
a.marshCommented:
Here's a complete example of doing just what you want - it goes a step further and uses the actual fieldnames and values of the query string:


index.html
----------

<html>
<head>
<title>Enter your details</title>
</head>
<body>
<form action="story.html">
<table border="0" cellpadding="5" cellspacing="5">
<tr><td>Your name</td><td><input type="text" name="your_name" value=""></td></tr>
<tr><td>First friend</td><td><input type="text" name="friend1" value=""></td></tr>
<tr><td>Second Friend</td><td><input type="text" name="friend2" value=""></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" value="create story..."</td></tr>
</table>
</form>
</body>
</html>



story.html
----------

<html>
<head>
<title>The Story</title>
<script language="javascript">

your_name = "test";
friend1 = "";
friend2 = "";

// Get names
var queryString = window.location.href.substring((window.location.href.indexOf('?') + 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(tempArray[0] + " = \"" + tempArray[1] + "\"");
}

</script>
</head>
<body>
<p>This is the story of
<script language="javascript"> document.write(your_name);</script>'s amazing adventure with
<script language="javascript"> document.write(friend1);</script> and
<script language="javascript"> document.write(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(your_name);</script>,
<script language="javascript"> document.write(friend1);</script> and
<script language="javascript"> document.write(friend2);</script>'s adventure!
</p>
</form>
</body>
</html>


:o)

Ant
0
 
weiyee08Author Commented:
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 ?
0
 
a.marshCommented:
Basically:

var queryString = window.location.href.substring((window.location.href.indexOf('?') + 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
0
 
weiyee08Author Commented:
i kow it is working, but why you don't use array to display the name ?
0
 
avnerCommented:
weiyee08 ,
I would change the code to use Hash-Table instead of Array.
0
 
a.marshCommented:
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
0
 
weiyee08Author Commented:
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 ?
0
 
a.marshCommented:
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
0
 
a.marshCommented:
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
0
 
weiyee08Author Commented:
a.marsh,

I don't mind if you answer his idea as long as you are the one who answer first
0
 
a.marshCommented:
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.substring((window.location.href.indexOf('?') + 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"]);</script>'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"]);</script>,
<script language="javascript"> document.write(nameArray["friend1"]);</script> and
<script language="javascript"> document.write(nameArray["friend2"]);</script>'s adventure!
</p>
</form>
</body>
</html>


Ant
0
 
weiyee08Author Commented:
a.marsh

I think you are right. Thanks !
0
 
a.marshCommented:
Glad to help.

:o)

Ant
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 9
  • 7
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now