I have the below java script function in which I want to redirect the page to another page. I verified that 'Got' message is shown by the alert function. So, the function is invoked properly. However, the page redirection with window.location.href does not work. Moreover, the alert function that prints the content of option, vDaeId, dgpId does not executed (I dont see a pop up in the browser). Could you please tell me what's wrong with this piece of code? Thanks. I am using firefox btw.
function goToPMDisplayer(){
alert('Got ');
var option=document.getElementById("D1").value;
var vDaeId=document.getElementById("D2").value;
var dgpId=document.getElementById("dgpids").value;
var str= option + " "+ vDaeId + " "+ dgpId
alert(str);
window.location.href="display.jsp?option="+option + "&vdaeid=" + vDaeId + "&dgpid=" + dgpId
}
I have the below java script function in which I want to redirect the page to another page. I verified that 'Got' message is shown by the alert function. So, the function is invoked properly. However, the page redirection with window.location.href does not work. Moreover, the alert function that prints the content of option, vDaeId, dgpId does not executed (I dont see a pop up in the browser). Could you please tell me what's wrong with this piece of code? Thanks. I am using firefox btw.
function goToPMDisplayer(){
alert('Got ');
var option=document.getElementById("D1").value;
var vDaeId=document.getElementById("D2").value;
var dgpId=document.getElementById("dgpids").value;
var str= option + " "+ vDaeId + " "+ dgpId
alert(str);
window.location.href="display.jsp?option="+option + "&vdaeid=" + vDaeId + "&dgpid=" + dgpId
}
Share
Improve this question
asked May 16, 2012 at 14:08
F. AydemirF. Aydemir
2,7355 gold badges43 silver badges62 bronze badges
10
- 1 are there missing ";"s ? – Sebas Commented May 16, 2012 at 14:09
- 1 @Sebas - Not a problem in Javascript. – Oded Commented May 16, 2012 at 14:09
-
2
If the
alert(str)
doesn't work, that means there's an error on one of yourvar
lines`. My guess is one of those elements don't exist. – gen_Eric Commented May 16, 2012 at 14:10 - I just added semi colonns. No change. Any other suggestion? – F. Aydemir Commented May 16, 2012 at 14:10
-
1
try putting this script just before
</body>
. That should make sure the elements exist before execution. – Joseph Commented May 16, 2012 at 14:10
2 Answers
Reset to default 5You said the first alert works, and the second doesn't, that means one of these lines has a javascript error:
var option=document.getElementById("D1").value;
var vDaeId=document.getElementById("D2").value;
var dgpId=document.getElementById("dgpids").value;
The most likely culprit is that one of these elements does not exist, and attempting to access .value
of null
will throw an exception.
Moreover, the alert function that prints the content of option, vDaeId, dgpId does not executed (I dont see a pop up in the browser).
That tells us that the code is failing by throwing an exception. My guess would be that you don't have at least one of the elements you're relying on having (D1
, D2
, or dgpids
). So document.getElementById
returns null
, and when you try to access .value
on it, you get an exception.
Note that the strings you use with getElementById
must be id
values, not name
s. So you must have id="D1"
on one element, id="D2"
on another, and id="dgpids"
on another. Also note that id
values can be case-sensitive, and that they must be unique on the page. (Most browsers will give you the first one in document order if you mess up that last rule, but...)
I assume that you're not calling goToPMDisplayer
until the page is fully loaded, but if you're calling it during page load, make sure you're not calling it until after the elements exist (e.g., put the call to it lower in the document than the elements).
For problems like this, alert
-style debugging went out some years ago. Here in 2012, we use proper debuggers. There's one built into all major browsers these days, even IE. You can set break points, walk through code, inspect the current state of the DOM... In short, no need to fumble about in the dark; you can shine light on what's happening.
In a ment on the question, you've said
I verified that all ements exist.
With all due respect, I just don't see how that can be. I suspect the id
values are slightly different, or that they don't have id
values at all, but rather they have name
values.
Here's a plete, working example: Live copy | source
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis./ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>Test Page</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<input id="D1" value="valueOfD1">
<input id="D2" value="valueOfD2">
<input id="dgpids" value="valueOfdgpids">
<br><input id="theButton" type="button" value="Click Me">
<script>
function goToPMDisplayer(){
alert('Got ');
var option=document.getElementById("D1").value;
var vDaeId=document.getElementById("D2").value;
var dgpId=document.getElementById("dgpids").value;
var str= option + " "+ vDaeId + " "+ dgpId
alert(str);
var loc = "display.jsp?option="+option + "&vdaeid=" + vDaeId + "&dgpid=" + dgpId
alert("loc = " + loc);
}
document.getElementById("theButton").onclick = goToPMDisplayer;
</script>
</body>
</html>