Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Passing parameters to Javascript

Posted on 2000-03-23
11
562 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

789 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