views:

45

answers:

2

I have two html pages example1.html and example2.html, how will i pass variables from example1.html to example2.html using query string and retreive that variable in example2.html without using any serverside code

+1  A: 

You can make you of

  • Cookies
  • Querystring

to exchange values.

Pranay Rana
+1  A: 

In example1.html:

<a href='example2.html?myVar1=42'>a link</a>
<a href='example2.html?myVar1=43'>another link</a>

or generate the links with Javascript as desired. Just make sure the ?varName=value gets onto the end of example2.html somehow.

Then, in example2.html, you use Javascript to parse the query string that example2 came with.

To do this, you could try Querystring .

// Adapted from examples on the Querystring homepage.
var qs = new Querystring();
var v1 = qs.get("myVar1");

Alternatively, parent.document.URL contains the complete URI for the page you're on. You could extract that:

parent.document.URL.substring(parent.document.URL.indexOf('?'), parent.document.URL.length);

and parse it manually to pull the variables you encoded into the URI.

EDIT: Forgot the 'new' part of 'new Querystring()'. Oops...

cubic1271
Thanks for the reply i tried the second method what you suggested its working fine , i have writtten like var a=parent.document.URL.substring(parent.document.URL.indexOf('myVar1='), parent.document.URL.length);alert(a);
mahesh
But the problem is i have to store only 42 into variable a ,when i write alert(a); it displays myVar1=42 ,but how will i display only 42
mahesh