SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check is querystring variable exists

    I would like to check the querystring to see if a variable exists.

    EXAMPLE:
    www.somesite.com?groupid=123

    If groupid exists in the querystring and has ANY value then show a hidden div (#closedMsg). I do not care what the value of the groupid is I just want to verify that it has a value.

    If the url looks like this "www.somesite.com?groupid=" with no value for groupid, I do not want to show the hidden div.

    Here is my code so far which targets for a value of 123, how do I make it accept any value for groupid?

    Code:
    <script type="text/javascript" charset="utf-8">
    		// ***this goes on the global scope
    // get querystring as an array split on "&"
    var querystring = location.search.replace( '?', '' ).split( '&' );
    // declare object
    var queryObj = {};
    // loop through each name-value pair and populate object
    for ( var i=0; i<querystring.length; i++ ) {
          // get name and value
          var name = querystring[i].split('=')[0];
          var value = querystring[i].split('=')[1];
          // populate object
          queryObj[name] = value;
    }
    // ***now you can use queryObj["<name>"] to get the value of a url
    // ***variable
    if ( queryObj[ "groupid" ] === "123" ) {	 
    	 var $messageDiv = $('#closedMsg'); // get the reference of the div
    	 $messageDiv.show().html('yay, value of groupid exists');
    }
    </script>
    THANKS EVERYONE!

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You just need to do this:

    Code javascript:
    if (queryObj['groupid']) {
      // groupid is set
    }
    That's basically checking "does the groupid exist in the queryObj object"?

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    canada
    Posts
    274
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Awesome, thanks, works great!!


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
  •