Advanced Search

Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 32

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

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

    Default

    It means that the prototype $ function isn't valid too?? And is it a should or a must??
    "Should," not "must." It's just bad form.
    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. #22
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for clearing the point Twey.

  3. #23
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    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.
    You're not wrong. There would be overhead, of course, but then a counter argument is that the function would be called so infrequently that the relatively small amount of overhead that would occur would be insignificant. There's no harm in caching the results (though calling code could do that, too), but I don't think it's a big deal.

    Also, that script fails to take into consideration the non-existance of window.location.search,
    An alternative is the URL property of the document object, which should also be well supported.


    Quote Originally Posted by DimX
    Semicolons? I've never seen any site where the get variables are seperated with semicolons.
    That's not quite the point though, is it? The HTML Specification recommends in appendix B.2.2 that both HTTP and CGI (or other server-side language) implementors accept semicolons to allow markup authors to forego the hassle of encoding ampersands as entity references in URLs in attributes.

    var $_GET = new Array();
    Why an array? You aren't using any array features, but rather a feature of objects in general; an Object object is all that's necessary.


    Quote Originally Posted by Twey
    $ should be reserved for system identifiers in Javascript ...
    Machine-generated identifiers.

    and I still don't see what's wrong with my original:
    You use a for..in loop to iterate over an array. That's not particularly sensible, particularly as for..in iterative statements are prone to enumerating user-defined properties inherited through the prototype chain, and even host properties that aren't assigned the DontEnum attribute. You should use a for statement, instead, particularly due to the fact that the only properties you are interested in are array indices.


    Quote Originally Posted by shachi
    It means that the prototype $ function isn't valid too??
    The Prototype library is poor full-stop. The fact that it flouts an established convention is just one reason why.

    And is it a should or a must??
    It is defined as such in the language specification, and there is no reason to disregard that reservation: there are other, less obtuse means of naming or prefixing identifiers.

    Mike

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

    Default

    Quote Originally Posted by mwinter
    There would be overhead, of course, but then a counter argument is that the function would be called so infrequently that the relatively small amount of overhead that would occur would be insignificant.
    Yes, but the point was that one might as well save that overhead anyway, particularly when it takes more code to do it the slightly-less-efficient way.
    You use a for..in loop to iterate over an array.
    I do? Whoops...
    Code:
    <script type="text/javascript">
      var _GET = window.location.href.substr(window.location.href.indexOf('?') + 1).split(/&|;/);
      for(var i = 0; i < _GET.length; ++i)
        if(_GET[i].indexOf('=') === -1)
          _GET[unescape(_GET[i])] = '';
        else _GET[unescape(_GET[i].substr(0, _GET[i].indexOf('=')))] = unescape(_GET[i].substr(_GET[i].indexOf('=') + 1));
    </script>
    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!

  5. #25
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Yes, but the point was that one might as well save that overhead anyway, particularly when it takes more code to do it the slightly-less-efficient way.
    Umm:

    Code:
    function getQueryValue(name) {
        var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
    
        return match ? unescape(match[1]) : null;
    }
    More code, you say? That (only lightly tested, by the way) also copes with fragment identifiers, which seem to have been forgotten in other solutions.

    Mike

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

    Default

    Bah. Foiled. Well, that pretty much obsoletes the rest.
    I'm not as up on regex as I should be, so I have to ask: Could one use a .match() operation or two to fill an object with the values as I did with mine?
    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!

  7. #27
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    I'm not as up on regex as I should be, so I have to ask: Could one use a .match() operation or two to fill an object with the values as I did with mine?
    You could, though it wouldn't be a simple one-step process.

    When a string or non-global regular expression object is passed to the String.prototype.match method, it's exactly the same as calling the RegExp.prototype.exec method:

    Code:
    pattern.exec(string)
    If pattern was a string, it's passed to the RegExp constructor function, first.

    On the other hand, if the argument is a regular expression object with the global flag set, the return value is array of matches. Unfortunately, each element is the matched substring, rather than the actual result from the exec method. For example, if you were to use the regular expression:

    Code:
    /[?&;]([^=]+)=([^&;#]*)/
    you'd get results like "?foo=bar" and "&flag="; the capturing parentheses are ignored and might as well not exist. As such, you'd still need to strip the first character and split on the equals symbol.

    By the way, the latter result is how flags have to represented in the query string to be matched by the getQueryValue function. Furthermore, to differentiate a flag (which would be returned as an empty string) and a non-existent value, compare the return value with null.

    Mike

  8. #28
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Can you please explain why in this function:
    Code:
    function getQueryValue(name) {
        var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
    
        return match ? unescape(match[1]) : null;
    }
    you used this line:
    Code:
    return match ? unescape(match[1]) : null;
    while
    Code:
    return match[1];
    works fine?

    What exactly does the syntax mean?

  9. #29
    Join Date
    Jun 2006
    Posts
    182
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default

    It means:
    Code:
    if (match)
        return unescape(match[1]);
    else
        return null;
    Because if no matches are found it's better to return null.

  10. #30
    Join Date
    Jan 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok, so why the unescape() if it works without it?

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
  •