Solved

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

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

732 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