|
Post by Xephra on Jan 16, 2006 6:05:47 GMT
This is my equivalent of PHPs $_GET.
It's basic, I might do some other things soon.
<script> function jsGet(type){ if(location.href.match(type)){ return location.href.split(type+'=')[1].split('&')[0]; }} </script>
Example Useage:
document.write(jsGet('action'));
To show the current action.
=)
|
|
Peter
New Member
God
Posts: 6
|
Post by Peter on Jan 16, 2006 19:50:46 GMT
If you are trying to emulate PHP's $_GET, then maybe something like this would work better... var $_GET = new Object(); var $qry_str = location.search.substring(1); var $k = 0;
do { $_GET[$qry_str.split("=")[0].replace(/&/, "")] = $qry_str.split("=")[1].split(/&|$/)[0]; $qry_str = $qry_str.split($qry_str.split("=")[1].split(/&|$/)[0])[1]; $k ++; } while($k < (location.search.split("=").length - 1));
for(key in $_GET){ alert("Variable: " + key + "\nValue: " + $_GET[key]); }
|
|
|
Post by Xephra on Jan 16, 2006 20:17:45 GMT
Haha...I was thinking of doing a more advanced one, and some other things similar. Nice code. =)
|
|
|
Post by california on Jan 18, 2006 2:42:27 GMT
I didn't realize that $ was a valid character in JavaScript variables
|
|
|
Post by Eric on Jan 18, 2006 2:46:25 GMT
I didn't realize that $ was a valid character in JavaScript variables Yup. Its kind of bizarre (sp?) if you ask me. My var version (just a few changes): var $_GET = {}; vars_area = location.search.substring(1);
if(vars_area.length > 2) { get_sets = query_str.split("&"); for(i = 0, num_gets = get_sets.length; i < num_gets; i++) { parts = get_sets[$i].split("="); $_GET[unescape(parts[0])] = unescape(parts[1]); } }My function version: function getGetVar(searchStr) { start = location.search.indexOf(searchStr)+searchStr.length+1; end = (location.search.indexOf('&', start) == -1)? location.search.length : location.search.indexOf('&', start); return unescape(location.search.substring(start, end)); }
|
|
R/E/T/N
Full Member
PM me to be a coding mod and work with some of the best designers on a great forum.
Posts: 245
|
Post by R/E/T/N on Jan 19, 2006 1:28:48 GMT
Hm.. SO this is suposed to make a Javascript code do something that a PhP code does? Sorry, i'm prett clueless
|
|
|
Post by Eric on Jan 19, 2006 1:51:19 GMT
Hm.. SO this is suposed to make a Javascript code do something that a PhP code does? Sorry, i'm prett clueless aye, pretty much
|
|
|
Post by barkermn01 on Feb 26, 2009 17:01:33 GMT
Just one one Note i used this about 2 years ago and then came across needing it again today but it fails validation due to it creating an error if there are not any url variables So i fixed it for you hehe:
var $_GET = new Object(); var $qry_str = location.search.substring(1); var $k = 0;
if($qry_str.split("=")[1]){ do { $_GET[$qry_str.split("=")[0].replace(/&/, "")] = $qry_str.split("=")[1].split(/&|$/)[0]; $qry_str = $qry_str.split($qry_str.split("=")[1].split(/&|$/)[0])[1]; $k ++; } while($k < (location.search.split("=").length - 1)); }
Just added the if around it if($qry_str.split("=")[1]){
|
|