Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How to use Querystrings with JS

    Hi,
    With ASP when I want get a value via links, or what have you, I do this...

    request("this") or request.querystring("this")

    My question is, is there a way to do this with javascript and if so how?

    Thanks,
    Larry

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    617
    Thanks
    1
    Thanked 20 Times in 20 Posts
    The following picks up the ENTIRE query string including the leading question (?) mark:

    q=document.location.search.substring(0);

    To skip the question mark:

    q=document.location.search.substring(1);

    Either way, then you have to parse the string:

    a=new Array();
    a=q.split('&');

    Each item in the array will contain a lable, an equals (=) sign, and a value. Split them up with:

    b=new Array();
    b=a[index].split('=');

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    I like using this setup:

    <a href="=querrystring2.htm?size=XXXL&type=Cotton%20T%20Shirt">test</a>

    <script type="text/javascript">
    <!--//
    if (location.search){
    var vals=location.search.substr(1).split("&");
    var request= new Array();

    for (var i in vals) {
    vals[i] = vals[i].replace(/\+/g, " ").split("=");
    request[unescape(vals[i][0])] =unescape(vals[i][1]);
    }
    }

    //-->
    </script>


    Then you can just use javascript to refer to items like so:

    request["size"]

    which with this example would return XXXL

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts
    JohnKrutsch,
    That is awesome that's exactly what I was looking for.

    Thanks a Bunch,
    Larry

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That does indeed look handy... kind of a way to fool javascript into acting like ASP. If I need to use stuff from the querystring without a server-side language, I'm saving this!

    P.S. That means Jalarie's too... that also looks like a very straightforward way of doing it!

    Last edited by whammy; 06-20-2002 at 01:09 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #6
    Dave Clark
    Guest
    I use these that I wrote:

    // Copyright (c) 2001 by Dave Clark Consulting, Springdale, AR -- www.DaveClarkConsulting.com -- all rights reserved.
    // ---------------------------------------- //
    // Constructor function creates hash array from Search String.
    // Use as follows, e.g.:
    // var QueryString = new SearchStringExtract(self.location);
    // var QueryString = new SearchStringExtract(window.location);
    // var QueryString = new SearchStringExtract(self.top.location);
    // var QueryString = new SearchStringExtract(window.top.location);
    //
    function SearchStringExtract(locObj) {
    var i, inpt = locObj.search.substr(1);
    if (inpt.length > 0) {
    var ary = inpt.replace(/\+/g, " ").split("&");
    for (i in ary) {
    ary[i] = ary[i].split("=");
    this[unescape(ary[i][0])] = unescape(ary[i][1]);
    }
    }
    }
    // ---------------------------------------- //
    // Normal function to process Search String into global variables.
    // Use as follows, e.g.:
    // processSearchString(self.location);
    // processSearchString(window.location);
    // processSearchString(self.top.location);
    // processSearchString(window.top.location);
    //
    function processSearchString(locObj) {
    var i, inpt = locObj.search.substr(1);
    if (inpt.length > 0) {
    var ary = inpt.replace(/\+/g, " ").split("&");
    for (i in ary) {
    ary[i] = ary[i].split("=");
    window[ary[i][0]] = unescape(ary[i][1]);
    }
    }
    }
    // ---------------------------------------- //

    Free to use -- not to sell.

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sweet... the javascript fairy has shown up!

    (P.S. I didn't stick you with that moniker, Dave... heheh)

    I'm going to be messing with this stuff for awhile... but what a wealth of querystring information.

    Thank $MS that they made it easy on the server-side.

    P.S. How easy is this in PHP? (I'm sure someone will pipe up here!).

    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #8
    Dave Clark
    Guest
    Originally posted by whammy
    Sweet... the javascript fairy has shown up!

    (P.S. I didn't stick you with that moniker, Dave... heheh)
    And just who, might I ask, did?!?
    And, who's been talkin' 'bout me behind my back?
    And, what did I do to deserve that moniker?!?

    No Fear! I've got it made in the shade! I'm cool!

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by whammy
    P.S. How easy is this in PHP? (I'm sure someone will pipe up here!).
    You asked for it:

    1.
    in_array("needle", $HTTP_GET_VARS);

    2.
    extract($HTTP_GET_VARS);

    Newer PHP versions do use $_GET, my example should be backwards compatible with older PHP versions.

    And yes, I know I'm slightly offtopic here. CNR.

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    Actually in PHP you do not have to do anything special at all to get info out of the query string. If you are at this url:

    http://www.someWebStore.com/products...on%20T%20Shirt


    all you would need to do to access the information is to simply ask for the item like so:

    $size

    so to print the size that was passed in the querystring in php you would only have to go like this:

    echo $size;

    Php does all of the hard work for you, you dont even have to "request" it like in ASP. It figures that it must be a variable you want to use since you passed it along in the first place.

    BUT I DIGRESS...after all this is hte JavaScript forum

  • #11
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Going further offtopic, but I have to respond:

    JohnKrutsch, what you said about PHP was true before the recent version 4.2 disabled register_globals in the default .ini file. This was done due to security reasons. Of course you may still change this .ini setting, but I don't know if it's the best idea to override the advice of the PHP developing team.

  • #12
    Dave Clark
    Guest
    Originally posted by JohnKrutsch
    Php does all of the hard work for you, you dont even have to "request" it like in ASP. It figures that it must be a variable you want to use since you passed it along in the first place.
    Technically, you aren't "requesting" anything from within ASP either. ASP also does all the "hard" work for you. The only difference is that PHP chose to extract the query string into unstructured individual variables that you reference individually and ASP chose to extract the query string into an object structure from which you reference the data collectively.

    <soapbox>
    It's too bad that people have to take occasional pot-shots at other languages, presumably, just because they don't happen to like that language. Every language has its value both in application and to the people using it. It is not necessary (or polite) to have to "tear down" one thing in order to try and "build up" another thing.
    </soapbox>

    OK?

  • #13
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hmm.. I don't see any pot shots being taken here. I simply see people disscussing the different ways to get info out of the querystring.

    I am one of the few people I know who can actually effectively develop web applications on an IIS box using ASP and SQL Server or MS Access, as well as, PHP/mySQL on an Apache server. I let my clients dictate that.

    If my comments were viewed as "tearing down" ASP in order to "build up" PHP that was not my intention. I was simply making a comparison.

    I do however agree whole heartedly with you that when this is the case it only stirs the hornets nest.

    My appoligies if my intentions were unclear. You should all know that Dave Clark is one of the best scrippters I have had the good fortune to associate myself with. His contirbutions have made me a better scriptter. IMHO when Dave Gets on a soap box it would be wise to listen.

  • #14
    Dave Clark
    Guest
    Originally posted by JohnKrutsch
    My appoligies if my intentions were unclear.
    OK, peace. But, John, I feel like I've been put on a pedestal that I don't deserve. Besides, it's embarrassing.

  • #15
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've made a little script like John's for those too high on ASP to notice the discrepancy(sp?) between ASP and JS :

    Code:
    function QSHandler() {
    	var qs=location.search.substr(1).split("&");
    	this.data=[];
    	for(var i=0;i<qs.length;i++) this.data[qs[i].split("=")[0]]=qs[i].split("=")[1];
    
    this.QueryString=function(x) {
    return this.data[x];
    	};
    } var Request=new QSHandler();
    You can get querystring values like so:

    Code:
    var name=Request.QueryString("name");
    Hope that helps!

    Happy coding!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •