[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Evaluating Javascript URL Parameter

Posted on 2009-04-30
1
Medium Priority
?
1,504 Views
Last Modified: 2012-05-06
So i have posted the code but basicly i want to add a url parameter and have javascript be able to grab and read it.  So this part works, i can get the parameter and write it out with

document.write("type=" + g_querystring["type"] + "<br>");

But what i want to do is run some conditional statements to do different things specifically hide the div with the id of menu if the parameter is a certain thing.

So if i have ?type=arth on my url then the menu div should be hidden

if(g_querystring["type"] == "arth")
{
  var mydiv=document.getElementById("menu");
  mydiv.style.visibility="hidden";
  mydiv.style.display="none";
  document.write("Yea worked");
}

The hidding the div part etc should be fine but the condition i cant get to evaluate as true or correct or in other words even if i have ?type=arth on my url the line of code if(g_querystring["type"] == "arth") doesnt seem to evaluate as true and run the options there
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
 
  <script type="text/javascript">
function parseQueryString()
{
var obj = new Object();
 
var nvpairs = location.search.substring(1).split("&");
 
for (var idx = 0; idx < nvpairs.length; idx++)
{
var tokens = nvpairs[idx].split("=");
 
obj[unescape(tokens[0])] = tokens.length == 2 ?
unescape(tokens[1]) : undefined;
}
 
return obj;
}
 
var g_querystring = parseQueryString();
 
document.write("type=" + g_querystring["type"] + "<br>");
 
// Iterate through all name/value pairs
 
if(g_querystring["type"] == "arth")
{
  var mydiv=document.getElementById("menu");
  mydiv.style.visibility="hidden";
  mydiv.style.display="none";
  document.write("Yea worked");
}
</script>
</head>
 
<body>
<div id="menu">I am the menu button</div>
 
<div id="body">I am the body information </div>
 
<div id="footer">I am the footer information</div>
 
 
</body>
</html>

Open in new window

0
Comment
Question by:Brant Snow
1 Comment
 
LVL 4

Accepted Solution

by:
jwmcpeak earned 2000 total points
ID: 24272662
It's not hiding the element because that element doesn't exist in the document yet; the JavaScript is executing before the browser parsed and loaded the HTML into the DOM. Change the last of your code (the if statement) to this:
onload = function() {
    if(g_querystring["type"] == "arth")
    {
        var mydiv=document.getElementById("menu");
        mydiv.style.visibility="hidden";
        mydiv.style.display="none";
        //document.write("Yea worked");
    }
};

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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

829 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