Solved

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

Posted on 2006-10-19
3
212 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
  • 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 500 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

777 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