Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

707 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