Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading parameters from URL

Status
Not open for further replies.

kavukattu

Programmer
Apr 29, 2002
13
US
I have a quick question, let's say I am passing parameters to an html page as follows:
mypage.html?VAR1=SOMEVALUE&VAR2=10

In javascript, how do I capture those variables?

Thanks
Toby
 
you could have an accessor for these values.

here is a funky solution! :) LOL

<html>
<head>
<title>get parameters and make them JS variables</title>
<script>

var paramKeys = new Array();
var paramValues = new Array();

function getParams()
{
var text = location.href
var hasParams = (text.indexOf(&quot;?&quot;) != -1)
var params = (hasParams) ? unescape(text.substring(text.indexOf('?')+1, text.length-1)) : &quot;&quot; ;
var couples = params.split(&quot;&&quot;);

for (var idx = 0; idx < couples.length; idx++)
{
var couple = couples[idx].split(&quot;=&quot;);
paramKeys.push(couple[0]);
paramValues.push(couple[1]);
}
}
</script>

</head>

<body onload=&quot;getParams()&quot;>


</body>
</html>

Hope this helps. Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top