Web Analytics Made Easy -
StatCounter How to use Querystrings with JS - CodingForum

Announcement

Collapse
No announcement yet.

How to use Querystrings with JS

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    "No Page To Show, Just Likes To Code."

  • #2
    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('=');
    Having said that, I've probably told you more than I know.

    Comment


    • #3
      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

      Comment


      • #4
        JohnKrutsch,
        That is awesome that's exactly what I was looking for.

        Thanks a Bunch,
        Larry
        "No Page To Show, Just Likes To Code."

        Comment


        • #5
          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; Jun 19, 2002, 09:09 PM.
          Former ASP Forum Moderator - I'm back!

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

          Comment


          • #6
            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.

            Comment


            • #7
              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. ;)

              Comment


              • #8
                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!

                Comment


                • #9
                  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.
                  De gustibus non est disputandum.

                  Comment


                  • #10
                    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

                    Comment


                    • #11
                      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.
                      De gustibus non est disputandum.

                      Comment


                      • #12
                        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?

                        Comment


                        • #13
                          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.

                          Comment


                          • #14
                            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.

                            Comment


                            • #15
                              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!

                              Comment

                              Working...
                              X