Help neede....
i want to read varaible value using java script.
For ex: url: http://www.test.com/myvar=goon&myvar2=stop
<script>
read myvar_value_here
read myvar2_value_here
</script>
please advice.
thanks
| SitePoint Sponsor |
Help neede....
i want to read varaible value using java script.
For ex: url: http://www.test.com/myvar=goon&myvar2=stop
<script>
read myvar_value_here
read myvar2_value_here
</script>
please advice.
thanks





Everyone repeat after me: "Google is my friend!"
This is one of about a zillion functions for reading GET parameters I found in a Google search:
http://javascript.about.com/library/blqs1.htmCode:var qsParm = new Array(); function qs() { var query = window.location.search.substring(1); var parms = query.split('&'); for (var i=0; i<parms.length; i++) { var pos = parms[i].indexOf('='); if (pos > 0) { var key = parms[i].substring(0,pos); var val = parms[i].substring(pos+1); qsParm[key] = val; } } }
Reading POST variables will be a little trickier. You might try using an onsubmit handler in your form to put the POST variables into a cookie. Then use a function on the receiving page to parse the cookie.
Thanks.
i thought there might be some other way out except reading the complete url ...
anyways thanks![]()



Well, if you are running php or some other server side script you could do something like this as well:
<?php?
$myvar=$_GET['myvar'];
$myvar2=$_GET['myvar2'];
echo "<script>";
echo "var myvar=$myvar";
echo "var myvar2=$myva2r";
echo "</script>";
?>
This is how I have retrieved post values in the past. The .js function is nodoubt easier though.




well I used to convert it into JSON format instead of pure echo back, if only you have simple data structure that you can do as above.
Bookmarks