开发者

How redirect cancel function in Javascript

I'm not a programmer. I don't want to protect with a strong secure code my page. I just need one option I'm开发者_JAVA技巧 missing in my code and can't figure out how to add it.

<script language="Javascript">
<!--

var password = "lala"

var x = prompt("","")

if (x.toLowerCase() == password) {
location = "http://google.com";
}

else {
alert("Fail")
location = "http://facebook.com"
}

//-->
</script>

As you can see it's so dump but I need it. When I press Cancel button instead of writing true or false text, website still opens. I want to include in this script cancel button function (control it, you know) whitch would redirect to another website if press on it (as it is with true or false functions). I don't want to creat a special button or an input for it.

Update: I would like to include this script in a page which i am redirecting to. Could anyone tell me: 1. How can i modify this script to make it work only once? 2. Is it anything to do with browser's cookies?

p.s. Done :)


If the user presses cancel, prompt will return null. So do like this:

if(x == null) // Cancel
{
    alert('Cancel');
}
else if (x.toLowerCase() == password) { // Correct password
    location = "http://google.com";
}
else { // Wrong password
    alert("Fail")
    location = "http://facebook.com"
}

However, I'm not sure if all browsers will return null when the user presses cancel. (I have tested in Opera)


Try

var x = prompt("","");

if( x == null || x == '')
    return;

if (x.toLowerCase() == password) {
    location = "http://google.com";
}

else {
    alert("Fail")
    location = "http://facebook.com"
}


To redirect your browser to a URL use following snippet in your Javascript:

top.location.href = "http://google.com";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜