Solved

Passing parameters to Javascript

Posted on 2000-03-23
11
560 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

773 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