Results 1 to 6 of 6

Thread: [DHTML] Url Variable/Value Lookup

  1. #1
    Join Date
    Aug 2006
    Posts
    30
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [DHTML] Url Variable/Value Lookup

    1) CODE TITLE: Url Variable/Value Lookup

    2) AUTHOR NAME/NOTES: Gavin Delphia 2007

    3) DESCRIPTION:

    This script can retrieve the value of variables passed through the url or it can do the reverse and find the variable with specified value.
    It can also check if the variable or value even exists in the url.
    It is compatible with firefox and Internet Explorer.

    UrlVariable(variable,0) - Returns the value of the variable or false is variable not found

    UrlVariable(value,1) - Returns the first variable found that matches the value or false is value not found


    4) http://javag.f2g.net/demo.html?test=...or=blue&age=18

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

    Default

    Woah Nelly, nice overkill but I'm afraid it doesn't comply with the standards very well. The semicolon (;) is a valid GET parameter separator as well. Your functions plus that feature can be written with one regex each:
    Code:
    function UrlVariable(varname, isvalue) {
      var m = location.href.substr(location.href.indexOf("?") + 1).match(new RegExp(isvalue ? "([^;&]+)=" + varname : varname + "=([^;&]+)"));
      return m && m[1];
    }
    If we wanted to be really standards-compliant, though, we'd have to work with multiple values to the same variable too:
    Code:
    function UrlVariable(varname, isvalue) {
      var m = location.href.substr(location.href.indexOf("?") + 1).match(new RegExp(isvalue ? "([^;&]+)=" + varname : varname + "=([^;&]+)", "g")),
        r = [];
    
      for(var i = 0, n = m.length; i < n; ++i)
        r.push(isvalue ? m[i].substr(0, m[i].indexOf("=")) : m[i].substr(m[i].indexOf("=") + 1));
    
      return r;
    }
    Either way, the code doesn't come close to looking like that mass of string manipulation you've got going on there.

    There was a much more elegant implementation that mwinter wrote at one point, but I can't find it now.
    Last edited by Twey; 07-17-2007 at 11:11 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
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Default

    Ah yes, I forgot about # again, didn't I Mike's doesn't handle multiple values, though, and has one noticeable bug (exhibited with URLs such as page.html?var1=val1#;var2=val2)... I'd be inclined to adapt mine:
    Code:
    function urlVariable(varname, isvalue) {
      var m = location.href.substr(location.href.indexOf("?") + 1),
        r = [];
    
      m = (m.substr(0, location.href.indexOf("#")) || m).match(new RegExp(isvalue ? "([^;&]+)=" + varname : varname + "=([^;&]+)", "g"));
    
      for(var i = 0, n = m.length; i < n; ++i)
        r.push(decodeURIComponent(isvalue ? m[i].substr(0, m[i].indexOf("=")) : m[i].substr(m[i].indexOf("=") + 1)));
    
      return r;
    }
    Last edited by Twey; 07-19-2007 at 05:53 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!

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

    Default

    Twey: what's the second argument(isvalue) for?

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

    Default

    The original code had two uses: to find the value of a property given its name, or to find the name of a property given its value. isvalue is the trigger to switch to the latter.
    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
  •