check URL parameter with Jquery

smfmetro10
smfmetro10 used Ask the Experts™
on
Hi,

I need to be able to check the URL parameter for "subjectId" and if it is a certain value (say like 6) then show a form.
Here is what I have so far but it doesnt work.

	<script>
function GetURLParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}​
</script>
<script>
var Id = GetURLParameter('subjectId');
if (Id = 6) {
	$("#contactForm").show();
			}
			else {
				$("#contactForm").hide ();
			}
</script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2008
Top Expert 2008
Commented:
<script>
function  GetURLParameter( name, context )
{
 if(!context)
  context=window.location.href;

  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "(^|[\\?]|&(amp;)?)"+name+"=([^&#]*)";
  var regex = new RegExp( regexS,"" );
  var results = regex.exec( context );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[3]);
}


// I am guessing you are trying to do this when the page loads
$(document).ready(function(){
  var Id = GetURLParameter('subjectId');

// you need at least TWO equal signs to check for equality
if (Id == 6) {
	$("#contactForm").show();
			}
			else {
				$("#contactForm").hide ();
			}
});
</script>

Open in new window

Author

Commented:
Thanks!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial