Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 32

Thread: Implementation to send variables through the url (with javascript) like PHP $_GET.

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

    Default

    Still bigger than it has to be.

    There's no point in calling a function every time you want to get a variable. The query string isn't going to change, so you might as well just store everything in an object at the start and avoid the overhead. Also, that script fails to take into consideration the non-existance of window.location.search, the use of semicolons to separate variables, and the use of URL-encoded characters.
    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!

  2. #12
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by Twey
    Also, that script fails to take into consideration the non-existance of window.location.search
    Actually, if there is no query string the function returns undefined. So I don't have to deal with that.

    Semicolons? I've never seen any site where the get variables are seperated with semicolons.

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

    Default

    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!

  4. #14
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh thank twey and DimX for improving my code. I was using it because of my non-serverside-language-enabled site in tripod and my code was limited but yours rocks. Thanks all you guyz. I think that it can even handle ajax requests. But I must try.Thanks again.

  5. #15
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by Twey
    Oh well, fine here's the "perfect" function:
    Code:
    function $_GET(key_str) {
        if(window.location.search) {
            var query = window.location.search.substr(1);
            var pairs = query.split(/&|;/);
            for(var i = 0; i < pairs.length; i++) {
                var pair = pairs[i].split("=");
                if(unescape(pair[0]) == key_str)
                    return unescape(pair[1]);
            }
        }
    }
    
    alert( $_GET("p") );
    Btw. yes, it could be done only once when the page loads:
    Code:
    var $_GET = new Array();
    if(window.location.search) {
        var query = window.location.search.substr(1);
        var pairs = query.split(/&|;/);
        for(var i = 0; i < pairs.length; i++) {
            var pair = pairs[i].split("=");
            $_GET[unescape(pair[0])] = unescape(pair[1]);
        }
    }
    
    alert( $_GET["p"] );
    Last edited by DimX; 08-20-2006 at 09:25 AM.

  6. #16
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    That's perfect DimX,Twey!!!

  7. #17
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Oh it does not work with ajax calls probably because it cannot find window.location in ajax requests.

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

    Default

    Quote Originally Posted by DimX
    var $_GET = new Array();
    if(window.location.search) {
    var query = window.location.search.substr(1);
    var pairs = query.split(/&|;/);
    for(var i = 0; i < pairs.length; i++) {
    var pair = pairs[i].split("=");
    $_GET[unescape(pair[0])] = unescape(pair[1]);
    }
    }

    alert( $_GET["p"] );
    $ should be reserved for system identifiers in Javascript, and I still don't see what's wrong with my original:
    Code:
    <script type="text/javascript">
      var _GET = location.href.substr(window.location.href.indexOf('?') + 1).split(/&|;/);
      for(var i in _GET)
        if(_GET[i].indexOf('=') === -1)
          _GET[decodeURIComponent(_GET[i])] = '';
        else _GET[decodeURIComponent(_GET[i].substr(0, _GET[i].indexOf('=')))] = decodeURIComponent(_GET[i].substr(_GET[i].indexOf('=') + 1));
    </script>
    Quote Originally Posted by shachi
    Oh it does not work with ajax calls probably because it cannot find window.location in ajax requests.
    It won't work with AJAX calls, because scripts on a page loaded via AJAX aren't even called unless you do so manually, and then they refer to the loading page's window.location, not the loaded page.
    Last edited by Twey; 07-27-2008 at 12:44 AM. Reason: Changed unescape() to decodeURIComponent().
    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!

  9. #19
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    and then they refer to the loading page's window.location, not the loaded page.
    That's what I meant by

    Quote Originally Posted by me
    probably because it cannot find window.location in ajax requests
    Quote Originally Posted by Twey
    $ should be reserved for system identifiers in Javascript,
    It means that the prototype $ function isn't valid too?? And is it a should or a must??
    Last edited by shachi; 08-20-2006 at 02:18 PM.

  10. #20
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    Quote Originally Posted by Twey
    ... and I still don't see what's wrong with my original
    Nothing, it's perfect, I was just using another technique

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
  •