Results 1 to 5 of 5

Thread: Get string out of location url

  1. #1
    Join Date
    Apr 2007
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Get string out of location url

    I got a CGI/Perl script that reads databases and displays the results on template pages.
    I need to be able to get the name of the database that is being read from the url location to be able to use it in a link in the template page.
    The URL would look somehting like this:
    perl.cgi?i=database&w=template&re=date&rada=012
    in witch I would like to get the i field from the URL.

    I heard an idea about creating a dummy <span> tag with an id, then within the span tag use document.write to write the location url into the span tag, then using the getElementById it might be possible to use substr(value1,value2) to identify the database call in the url.
    I just don't know how to do this if it works.

    Any ideas about what I could do to solve this problem?

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    String.prototype.getvar = function(query) {
      var value = this.substring(this.indexOf(query)+1,this.length),
        end = value.indexOf("&") ? value.indexOf("&") : value.length;
      return value.substring(value.indexOf(query)+query.length+1,end);
    };
    This basically works like this:
    String.getvar("variable")
    Code:
    document.write("perl.cgi?i=database&w=template&re=date&rada=012".getvar("i"));
    The above can be i, w, re or rada. Those are the variables in the string given.
    Last edited by mburt; 05-09-2007 at 10:42 PM.
    - Mike

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

    Default

    Quote Originally Posted by mburt View Post
    Code:
    String.prototype.getvar = function(query) {
      var value = this.substring(this.indexOf(query)+1,this.length);
        end = value.indexOf("&") ? value.indexOf("&") : value.length;
      return value.substring(value.indexOf(query)+query.length+1,end);
    };
    That is very fragile code. I posted a better alternative a couple of days ago. It will need to be modified slightly to process the correct string (that version works on the Referrer URL).

    Mike

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Oh, I never thought of using regular expressions... Neat.
    - Mike

  5. #5
    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

    I had seen mwinter's method of doing this a little earlier and came up with this version of it that allows you to optionally specify the string to be searched. It defaults to the location.search of the current page. Otherwise, it is pretty much a clone of mwinter's with a little error checking thrown in:

    Code:
    function getQval(n, m) {
    if(!arguments[0]||typeof n!='string')
    return null;
    var r=new RegExp('[?&;]'+n+'=([^&;#]*)'), m=arguments[1]?m:location.search;
    return (m=r.exec(m))? unescape(m[1]) : null;
    }
    - John
    ________________________

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

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
  •