We help IT Professionals succeed at work.

Passing parameters to Javascript

kerzner
kerzner asked
on
629 Views
Last Modified: 2012-06-22
How does one pass parameters to Javascript, similar to ASP?

In ASP, you can form a URL and add
?mypar=value, and get it in the ASP on the new page using Request object.

Is there a similar thing in Javascript?
Thanks
Comment
Watch Question

CERTIFIED EXPERT

Commented:
You can view the "querystring" in javascript like this:

alert( location.search );

Is that what you mean?
CERTIFIED EXPERT

Commented:
Here is a js function that will behave similar to the Request.QueryString method in .ASP:

<SCRIPT language='javascript'>

function querystring(name)   // returns a named value from the querystring
{
   var tmp = unescape( location.search.substring(1,location.search.length) );
   var i   = tmp.indexOf(name+"=");

   if ( i >= 0 )
   {
      tmp = tmp.substring( name.length+i+1, tmp.length );
      i = tmp.indexOf("&");
      return( tmp = tmp.substring( 0, (i>=0) ? i : tmp.length ));
   }

   return("");
}

</script>
CERTIFIED EXPERT

Commented:
... so if you pass a parameter to the page like this:  something.htm?myparam=blah

then this will put "blah" into mystr:

var mystr = querystring("myparam");
alert(mystr);

Commented:
.. what he said ... ;-)

Although you shouldn't "unescape" the query string right away in case there is a '&' in the value of what you're attempting to read.

squint
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
This is a great answer, and I have already accepted the one by knightEKnight, but it does not show for some reason.

Also, if no standard means are provided in JavaScript for this, I may end up using JSP, but those answer showing how to do it were very important.

Thank you.
CERTIFIED EXPERT

Commented:
Thanks kerzner,
If you found my suggestion most helpful, please contact EE about any problem you are having accepting my answer.

Author

Commented:
Thanks, I will. Besides, many times two people give an answer that in its total solves the problem, but I do not know of a provision to split points.

Author

Commented:
I checked the list of suggestions, it is on the list and is even suggested a number of times. Until then, I do not know what to do more.

Commented:
Thanks..
I really like to deal with Gentelmen like both of you.
CERTIFIED EXPERT

Commented:
Also, this function can be done quicker:

function myunescape(s){
  return unescape(s.split("+").join(" "));
}
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.