214-564-5316
Thursday, March 11, 2010

Pass variables in URL string with javascript
The window.location.search property lets you get the query string of a URL, for example http://yourserver/page.html?myvar=mydata. The question mark and everything after it is the query string. If you use <form method="GET" action="nextpage.html"> in a form, then any values in the form elements get put into the URL query string of the receiving page. From there all you have to do is parse that string to get your individual variable names and values out.
The code below just gets the length of the query string, then uses the substring() method to take out the initial "?myvar=", then it places that variable in the form field on the nexpage.html. Try it out by typing in something to the textbox and click submit, you will see it pass to the nextpage.html and be inserted into another text box. Look at the browser URL and you can see the variable up there also!


Here is the code for the two pages: