admin管理员组文章数量:1325367
I am new to the javascript window.location and I was trying to use it to direct to certain pages when certain phrases are entered into a prompt box. Is the way I did it below correct? For some reason it does not seem to work.
<html>
<head>
<script>
function desiredPg() {
var pgDesired = prompt("What do you want?");
if (pgDesired == "Log In" || "log in") {
window.location = "google";
} else if (pgDesired == "Register" || "register") {
window.location = "apple";
} else {
alert("Please enter a valid page name. Do not forget to use appropriate spaces and capitalize if necessary");
}
}
</script>
</head>
<body onLoad="desiredPg()">
I am new to the javascript window.location and I was trying to use it to direct to certain pages when certain phrases are entered into a prompt box. Is the way I did it below correct? For some reason it does not seem to work.
<html>
<head>
<script>
function desiredPg() {
var pgDesired = prompt("What do you want?");
if (pgDesired == "Log In" || "log in") {
window.location = "google.";
} else if (pgDesired == "Register" || "register") {
window.location = "apple.";
} else {
alert("Please enter a valid page name. Do not forget to use appropriate spaces and capitalize if necessary");
}
}
</script>
</head>
<body onLoad="desiredPg()">
Share
Improve this question
edited Jan 27, 2011 at 3:12
chromedude
asked Jan 27, 2011 at 3:03
chromedudechromedude
4,30216 gold badges69 silver badges96 bronze badges
5
- 2 are you calling the function at all? – KJYe.Name Commented Jan 27, 2011 at 3:06
-
It seems right to me...
window.location
redirects the browser to whatever you tell it to. – JCOC611 Commented Jan 27, 2011 at 3:06 - yeah, I call it on body load. I verified that worked before I added the if statement, but I will post the rest just in case – chromedude Commented Jan 27, 2011 at 3:07
- So when you type your text into the prompt box and click "OK", nothing happens? – Marc W Commented Jan 27, 2011 at 3:07
- pretty much, it sends me to my webhost's error page – chromedude Commented Jan 27, 2011 at 3:13
4 Answers
Reset to default 4You need to use absolute URLs if you want to go to another host, so simply prepend http://
to the string.
Convert the string to lower case so you only have to perform one parison.
I'd suggest that you use a hash, this cleans up the code a ton and needs only one if
condition:
function desiredPg() {
var pgDesired = prompt("What do you want?");
var sites = {'log in': 'http://google.', 'register':'http://apple.'};
if (typeof sites[pgDesired.toLowerCase()] !== 'undefined') {
window.location = sites[pgDesired.toLowerCase()];
} else {
alert("Please enter a valid page name.");
}
}
If you set window.location
to a path like 'apple.', the path is taken as a relative path, means, if you are currently viewing Google, it will search for 'google./path'.
Try the absolute path instead, including the protocol..
window.location = "http://apple.";
Setting aside whether it is a good idea to make users remember page names, there are two problems in the code.
First, you have to evaluate each condition in an if statement separately, so those would be like pgDesired=="A" || pgDesired=="B"
(though Jacob's solution is more robust)
Second, you need to use the absolutely path as ring0 mentioned. So window.location = "http://apple.";
One more nitpick: The "standard" way of doing this is through the href
property of window.location
:
window.location.href = "http://www.example./";
If you want to keep the current location out of the browser's history, use replace()
:
window.location.replace("http://www.example./");
本文标签: debuggingHow does javascript windowlocation workStack Overflow
版权声明:本文标题:debugging - How does javascript window.location work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742193381a2430627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论