Results 1 to 4 of 4

Thread: Emulate PHP $_GET

  1. #1
    Join Date
    Jan 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Emulate PHP $_GET

    Hello. First post here. Just sharing fresh code.

    I tried to reply to this thread:
    http://www.dynamicdrive.com/forums/s...ad.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...

    Code:
    $_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] || "");
    	}
    })();

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    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);
    Last edited by Twey; 01-31-2008 at 12:17 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    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:

    Code:
    function getQueryValue(name) {
        var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
    
        return match ? unescape(match[1]) : null;
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •