Solved

Passing parameters to Javascript

Posted on 2000-03-23
11
556 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
0
Comment
Question by:kerzner
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 2651038
You can view the "querystring" in javascript like this:

alert( location.search );

Is that what you mean?
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2651073
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>
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2651079
... 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);
0
 
LVL 2

Expert Comment

by:squint
ID: 2651349
.. 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
0
 
LVL 10

Accepted Solution

by:
kollegov earned 100 total points
ID: 2652015
Here is correct version to handle spaces in names/values and escaped names/values correctly


first split on pairs by &
than split pairs by =
than unescape names values
compare throgh all values with given name

<script>

function myunescape(s){
 for(var i=0;i<s.length;i++){
   if(s.charAt(i)=='+'){
     s=s.substring(0,i)+" "+s.substring(i+1);
   }
 }
 return unescape(s)
}


function querystring(name)
{tmp = location.search;
 while(tmp.length>1){
   tmp=tmp.substring(1); // chop first ? or &
   inx=tmp.indexOf("&");
   if(inx<0) inx=tmp.length;
   t=tmp.substring(0,inx);

   tmp=tmp.substring(inx);
   inx=t.indexOf("=");
   n=myunescape(t.substring(0,inx));
   v=myunescape(t.substring(inx+1));
   if(n==name){
     return v;
  }
 }
 return null
}

</script>

Tested with NN4.61 (Win NT)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:kerzner
ID: 2652066
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.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2653688
Thanks kerzner,
If you found my suggestion most helpful, please contact EE about any problem you are having accepting my answer.
0
 
LVL 1

Author Comment

by:kerzner
ID: 2653747
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.
0
 
LVL 1

Author Comment

by:kerzner
ID: 2653764
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.
0
 
LVL 10

Expert Comment

by:kollegov
ID: 2653992
Thanks..
I really like to deal with Gentelmen like both of you.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 2655177
Also, this function can be done quicker:

function myunescape(s){
  return unescape(s.split("+").join(" "));
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now