javascript 提醒消息并重定向(或不重定向)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6178081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alert a message and redirect (or not)
提问by Toni Michel Caubet
lets say i'm making a festival website, and if you enter today you will se the 2010 edition because the 2011 edition it's not finished,
假设我正在制作一个节日网站,如果您今天进入,您将看到 2010 版,因为 2011 版尚未完成,
And i want to let to my users know that they are seing the old website and that they will be redirected to the facebook page (untill the new website its finished),
我想让我的用户知道他们正在查看旧网站,并且他们将被重定向到 facebook 页面(直到新网站完成),
with this code i would be redirecting to our facebook page
使用此代码,我将重定向到我们的 Facebook 页面
<SCRIPT LANGUAGE='JavaScript'>
window.alert('This is the old edition of our webiste. The new one will be avaliable soon. You would be redirected')
window.location.href='http://facebook.com/ourfacebookpage';
</SCRIPT>
But how could i do to allow user staying? (example, cancel / stay here button) so user can see the old website (but he knows it's the old website)
但是我该怎么做才能让用户留下来?(例如,取消/留在此处按钮)以便用户可以看到旧网站(但他知道这是旧网站)
thanks a lot for any idea!
非常感谢您的任何想法!
回答by Thai
The simplest way is to use window.confirm()
. It's like alert() but it has 2 buttons: OK and Cancel. It returns true if the user pressed OK and returns false if the user pressed Cancel.
最简单的方法是使用window.confirm()
. 它类似于 alert() 但它有 2 个按钮:OK 和 Cancel。如果用户按下 OK 则返回 true,如果用户按下 Cancel 则返回 false。
<script>
if (window.confirm('This is the old edition of our webiste. The new one will be avaliable soon. You would be redirected')) {
window.location.href='http://facebook.com/ourfacebookpage';
}
</script>
But having a confirm window popup on page load may surprise users. You can make that more subtle by putting the message on your page (with link to your facebook page) instead of popping up the alert/confirm box, and have another link that removes that message from the page (maybe using JavaScript).
但是在页面加载时弹出确认窗口可能会让用户感到惊讶。您可以通过将消息放在您的页面上(带有指向您的 facebook 页面的链接)而不是弹出警报/确认框,并使用另一个从页面中删除该消息的链接(可能使用 JavaScript)来使其更加微妙。
回答by Yuriy Rozhovetskiy
Use an window.confirm
instead of the window.alert
使用一个window.confirm
而不是 window.alert
回答by Niklas
Use confirm
.
使用confirm
.
if (confirm('This is the old edition of our webiste. The new one will be avaliable soon. You would be redirected')){
window.location.href='http://facebook.com/ourfacebookpage';
}
Example: http://jsfiddle.net/niklasvh/hERqw/