Solved

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

Posted on 2006-10-19
3
210 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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. …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now