admin管理员组

文章数量:1326105

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
Add a ment  | 

4 Answers 4

Reset to default 4

You 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