PDA

View Full Version : Emulate PHP $_GET



metaw3
01-30-2008, 02:30 PM
Hello. First post here. Just sharing fresh code.

I tried to reply to this thread:
http://www.dynamicdrive.com/forums/showthread.php?t=11883
but it seemed locked: "you don't have the privileges to reply..."

That's the only page I found on the web almost about what I was playing with last night: emulating PHP $_GET with no other concern than making it readable and usable for a PHP developer not used to JS. So again: common JS coding guidelines as well as performance were not a concern. It's just an expermiment for fun.

Online: http://www.christianfecteau.com/echo/$_GET/$_GET.html
and attached...


$_GET = {}; // emualte PHP $_GET
(function(){ // by Christian Fecteau
var $params = location.search.substr(1).split('&');
for (var $i = 0; $i < $params.length; $i++) {
var $pair = $params[$i].split('=');
if ($pair[0])
$_GET[$pair[0]] = unescape($pair[1] || "");
}
})();

Twey
01-30-2008, 03:17 PM
Don't use $ for identifiers in JS -- it's reserved for machine-generated identifiers only. In addition, escape() and unescape() are deprecated: encodeURIComponent() (in this case) should be used instead. Too, order isn't important here so we can iterate backwards and squeeze a few more cycles out of this algorithm. Try:
var Functional = {
map: function(f, a) {
for(var r = [], i = a.length - 1; i >= 0; --i)
r[i] = a[i];
return r;
},

invertArgs: function(f, args) {
return function(o) {
return f.apply(o, args);
};
},

combine: function(a, b) {
return function() {
return a(b.apply(null, arguments));
};
}
};

function Hash(obj) {
if(obj)
this.update(obj);
}

Hash.prototype = {
update: function(o) {
for(var x in o)
if(o.hasOwnProperty(x))
this[x] = o[x];
return this;
}
};

Hash.fromArray = function(a) {
for(var r = new Hash(), i = a.length; i >= 0; --i)
r[a[0]] = r[a[1]];
return r;
};

var _GET = (function(loc) {
return Hash.fromArray(
Functional.map(
Functional.combine(
function(a) { return Functional.map(decodeURIComponent, a); },
Functional.invertArgs(loc.split, "=")),
loc.split(/[&;]/)));
})(location.search);

jscheuer1
01-31-2008, 05:42 AM
In addition, escape() and unescape() are deprecated: escapeURIComponent() (in this case)

Isn't that encodeURIComponent()?

Anyways, escape and unescape are fine, especially if you want backward compatibility. However the right way would I suppose be to write one's own functions employing escape and unescape for use only when the newer functions aren't native to the user agent.

I still like mwinter's simple method though:


function getQueryValue(name) {
var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);

return match ? unescape(match[1]) : null;
}

Twey
01-31-2008, 12:16 PM
Isn't that encodeURIComponent()?Yep. *furtive edit*
Anyways, escape and unescape are fine, especially if you want backward compatibility.They break for non-ASCII characters.
I still like mwinter's simple method though:Mike's is good, but not the same as what was being asked for here. Also, I prefer to write general code and then apply it to a given situation, rather than write code just for one situation.