Jump to content

$_get javascript


djp1988

Recommended Posts

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

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

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

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

Archived

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

×
×
  • Create New...