?
Solved

How to Pass Parameters from Address Bar.. Pls Help!

Posted on 2006-10-19
3
Medium Priority
?
217 Views
Last Modified: 2008-01-09
How do I pass parameters from the address bar into javascript variables? For example:

My address bar says:
http://www.domain-test.com?stk_no=999&stk_desc=white%20shirt

I want javascript to do:
document.write("Stock Number is "+stk_no);
document.write("Description is "+stk_desc);

Pls help.. Thanks in advance for all the help.
0
Comment
Question by:bemara57
[X]
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
  • 2
3 Comments
 

Author Comment

by:bemara57
ID: 17771107
Ok I found the answer.. a great function:

//Retrieve parameter from URL
function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if (strHref.indexOf("?") > -1){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for (var iParam = 0; iParam < aQueryString.length; iParam++){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

I have a little problem with this though. For some reason, it returns the parameter in all lower case and even literally puts %20 between the words, instead of spaces. Why is this and how do I change the function to return the exact parameter?
0
 

Author Comment

by:bemara57
ID: 17771115
oh I see why the lowercase, because the toLowerCase function in there which I took out. But why the %20?
0
 
LVL 30

Accepted Solution

by:
callrs earned 2000 total points
ID: 17771118
<script type="text/javascript">
// Extract parameters from a url
function Q(url, qs){
var query=url.substring(url.indexOf("?")+1)
if(!query.length) return "";
var els=query.split("&")
var params=[];
for (var i=0; i<els.length; i++) params[i]=els[i].split("=");
for (var i=0; i<params.length; i++) if(params[i][0]==qs) return unescape(params[i][1]);
return "";
}
var URL="http://www.domain-test.com?stk_no=999&stk_desc=white%20shirt"
//var URL=document.location.href
document.write("Stock Number is '"+Q(URL,"stk_no")+"'<br>");
document.write("Description is '"+Q(URL,"stk_desc")+"'<br>");
document.write("Etc. is '"+Q(URL,"etc.")+"'<br>");
</script>


(Could be much improved upon, but the basic logic is here)
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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. …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

764 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