Solved

Passing parameters to Javascript

Posted on 2000-03-23
11
555 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

706 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

20 Experts available now in Live!

Get 1:1 Help Now