Results 1 to 5 of 5

Thread: using window.location

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

    Default using window.location

    hello,

    The thing is, I'm using a perl database program and I am going print out many types of the database in one html file. This is no problem using <!--#include virtual="file.cgi?database=data" --> in a shtml file.

    But what I am hoping to accomplish is that I could write in the window location: file.shtml?process=something. Something would be written instead of data. Then I wouldn't have to create I new shtml file for each database. I'm pretty sure that this isn't possible with JavaScript but I was hoping someone here could maybe help me, that is if it is possible.

    Thank you

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by zrinity.com
    Code:
    <script>
    function getQueryVariable(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
          return pair[1];
        }
      } 
      alert('Query Variable ' + variable + ' not found');
    }
    </script>
    Now make a request to page.html?x=Hello

    Code:
    <script>
      alert( getQueryVariable("x") );
    </script>
    will allow you to gain access to the GET variables passed into the url address

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

    Default

    Thanks for the response. I'm not getting it to work. Do I use this with a .shtml file, or how do I use it?

    Say I wanted to get this location put into the page file: script.cgi?database=nr1&search=..

    and then and the x=hello query... how will do this?

    I hope I'm making myself clear enough, so that we understand each other.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    From what I understand, just put it in your .shtml file, put it in the body section[I think], and everything should be fine, then do visit the address you'd go to:
    *.shtml?x=Hello
    I hope this helps.
    Jeremy | jfein.net

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

    Default

    Do you mean that i should do this:

    <html>
    <head>
    <title></title>
    <script>
    function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
    return pair[1];
    }
    }
    alert('Query Variable ' + variable + ' not found');
    }
    </script></head>
    <body>


    <!--#include virtual="database.cgi?database=one&x=
    <script>alert( getQueryVariable("x") );</script>
    " -->

    </body>
    </html>

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
  •