djp1988 Posted August 18, 2008 Share Posted August 18, 2008 I am trying to set a variable in javascript what is the value of the $_GET variable in the url, how can i do this ? Link to comment Share on other sites More sharing options...
Lulzim Posted August 18, 2008 Share Posted August 18, 2008 here is a small example: <html><head><?phpif( isset($_GET['test']) ){ echo "<script type='text/javascript'>\n"; echo "var x = ".$_GET['test'].";\n"; echo "alert(x);\n"; echo "</script>\n";}?></head><body><p>something</p></body></html> Link to comment Share on other sites More sharing options...
djp1988 Posted August 18, 2008 Author Share Posted August 18, 2008 is there no way just to do it in javascript because my javascript is located in an external file, the way I have it now I have a <a name=""> in the page with the value of the get variable et to it by php, and an id so i can get the value in javascript, but i''d prefer to grab that variable directly from javascript Link to comment Share on other sites More sharing options...
Lulzim Posted August 18, 2008 Share Posted August 18, 2008 I don't think there is a way to get the values from query string, you will have to read the url and parse the variablesthere are a lot of examples out there Link to comment Share on other sites More sharing options...
Jesdisciple Posted August 18, 2008 Share Posted August 18, 2008 I wrote this a while back; see here for its origin, walkthrough, and demo. window.GET = function(){ var url = window.location.href; var array = url.indexOf('#') == -1 ? url.substring(url.indexOf('?') + 1).split(/&;/): url.substring(url.indexOf('?') + 1, url.indexOf('#')).split(/&;/); //URLs can be like either "sample.html?test1=hi&test2=bye" or //"sample.html?test1=hi;test2=bye" window._GET = {}; for(var i = 0; i < array.length; i++){ var assign = array[i].indexOf('='); if(assign == -1){ _GET[array[i]] = true;//if no value, treat as boolean }else{ _GET[array[i].substring(0, assign)] = array[i].substring(assign + 1); } }} Link to comment Share on other sites More sharing options...
justsomeguy Posted August 18, 2008 Share Posted August 18, 2008 Look at the properties of the window.location object:http://w3schools.com/htmldom/dom_obj_location.aspwindow.location.pathname, window.location.search, window.location.hash, etc. Link to comment Share on other sites More sharing options...
Jesdisciple Posted August 18, 2008 Share Posted August 18, 2008 I've improved my function based on JSG's above post. If the query string is empty, my earlier version takes the entire URL as the query string (-1 +1 == 0). window.GET = function(){ var array = window.location.search.substring(1).split(/&;/); /* URLs can be like either "sample.html?test1=hi&test2=bye" or "sample.html?test1=hi;test2=bye" */ window._GET = {}; for(var i = 0; i < array.length; i++){ var assign = array[i].indexOf('='); if(assign == -1){ _GET[array[i]] = true;//if no value, treat as boolean }else{ _GET[array[i].substring(0, assign)] = array[i].substring(assign + 1); } }} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.