Jump to content

Getting Variable Value


sairfan1

Recommended Posts

Hii have some url like this www.somename.com?myvar=100&myvar2=120i want to get value of variable named myvar through a client side java script function like thisfunction getVal(){ alert(<how to get value of myvar?>);}how can i get value of first or second variable.thanking you in advancergds

Link to comment
Share on other sites

The entire querystring can be found in the window.location.search property. You can use string matching to figure out individual variables.

q = window.location.search.substring(1);get = new Array();q = q.split("&");for (part in q) {	i = q[part];	i = i.split("=");	get[i[0]] = i[1];}//now all elements are as indexes of get, and can be accessed e.g. get["myvar"]

Link to comment
Share on other sites

I use the search part of the url for adressing,like this: www.myWebsite.com/index.html?start&message=this-page-is-ready&to=main&endand i get it so

var to;var message;function getTo(){to=window.location.search.slice(window.location.search.search('&to') + 3,window.location.search.search('&end'));}function getMessage(){message=window.location.search.slice(window.location.search.search('start&') + 6,window.location.search.search('&to'));}function writeMessage(){document.getElementById('to').innerHTML=to;document.getElementById('message').innerHTML=message;}function correct(){document.body.innerHTML=document.body.innerHTML.replace(/-/gi " ");}

the html code looks like this

<center><h2>Redirect</h2><p>To: <div id="to" /></p><p>Comment: <div id="message" /></p></center>

and the output would be like this

RedirectTo: mainComment: this page is ready

there could be some errors but it works just fine for me

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...