[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 574
  • Last Modified:

Passing parameters to Javascript

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
kerzner
Asked:
kerzner
  • 5
  • 3
  • 2
  • +1
1 Solution
 
knightEknightCommented:
You can view the "querystring" in javascript like this:

alert( location.search );

Is that what you mean?
0
 
knightEknightCommented:
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
 
knightEknightCommented:
... 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
squintCommented:
.. 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
 
kollegovCommented:
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
 
kerznerAuthor 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.
0
 
knightEknightCommented:
Thanks kerzner,
If you found my suggestion most helpful, please contact EE about any problem you are having accepting my answer.
0
 
kerznerAuthor 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.
0
 
kerznerAuthor 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.
0
 
kollegovCommented:
Thanks..
I really like to deal with Gentelmen like both of you.
0
 
knightEknightCommented:
Also, this function can be done quicker:

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now