jQuery 单击警报页面后如何重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16955019/
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
How to reload a page after the OK click on the Alert Page
提问by Nabeel Arshad
I need to reload the page after the OK button is clicked on the Alert box. I am using the following code for it
单击“警报”框上的“确定”按钮后,我需要重新加载页面。我正在使用以下代码
alert("Successful Message");
window.location.reload();
But the window is reloaded immediately after displaying the alertbox. Please Help me in it
但是在显示警报框后立即重新加载窗口。请帮助我
回答by Bojan Milic
Confirm gives you chance to click on cancel, and reload will not be done!
确认让您有机会点击取消,并且不会重新加载!
Instead, you can use something like this:
相反,您可以使用以下内容:
if(alert('Alert For your User!')){}
else window.location.reload();
This will display alert to your user, and when he clicks OK, it will return false and reload will be done! :) Also, the shorter version:
这将向您的用户显示警报,当他单击“确定”时,它将返回 false 并重新加载!:) 另外,较短的版本:
if(!alert('Alert For your User!')){window.location.reload();}
I hope that this helped!? :)
我希望这有帮助!?:)
回答by codeVerine
Use javascript confirm()
method instead of alert. It returns true
if the user clicked ok button and returns false
when user clicked on cancel button. Sample code will look like this :
使用 javascriptconfirm()
方法而不是警报。它返回true
如果用户点击OK按钮,返回false
当用户点击取消按钮。示例代码如下所示:
if(confirm('Successful Message')){
window.location.reload();
}
回答by Deepak
use confirm box instead....
使用确认框代替....
var r = confirm("Successful Message!");
if (r == true){
window.location.reload();
}
回答by Sobin Sunny
alert('Alert For your User!') ? "" : location.reload();
You can write above code in this format also.It seems quite decent
你也可以用这种格式写上面的代码。看起来很不错
回答by Denn
回答by glenn ferns
I may be wrong here but I had the same problem, after spending more time than I'm proud of I realised I had set chrome to block all pop ups and hence kept reloading without showing me the alert box. So close your window and open the page again.
我可能在这里错了,但我遇到了同样的问题,在花费了比我引以为豪的时间更多的时间之后,我意识到我已经设置了 chrome 来阻止所有弹出窗口,因此在没有向我显示警告框的情况下继续重新加载。所以关闭你的窗口并再次打开页面。
If that doesn't work then you problem might be something deeper because all the solutions already given should work.
如果这不起作用,那么您的问题可能更深,因为已经给出的所有解决方案都应该有效。
回答by Siraj Shaikh
Try this code..
试试这个代码..
IN PHP Code
在 PHP 代码中
echo "<script type='text/javascript'>".
"alert('Success to add the task to a project.');
location.reload;".
"</script>";
IN Javascript
在 JavaScript 中
function refresh()
{
alert("click ok to refresh page");
location.reload();
}
回答by Fiddlestiques
Interesting that Firefox will stop further processing of JavaScript after the relocate function. Chrome and IE will continue to display any other alerts and then reload the page. Try it:
有趣的是,Firefox 将在重定位功能后停止对 JavaScript 的进一步处理。Chrome 和 IE 将继续显示任何其他警报,然后重新加载页面。尝试一下:
<script type="text/javascript">
alert('foo');
window.location.reload(true);
alert('bar');
window.location.reload(true);
alert('foobar');
window.location.reload(true);
</script>
回答by Razen Kellesly
Try this:
尝试这个:
alert("Successful Message");
location.reload();
回答by CHess
As the alert method in JavaScript does not return a Boolean or yield the current thread, you must use a different method.
由于 JavaScript 中的 alert 方法不返回布尔值或生成当前线程,因此您必须使用不同的方法。
My number one recommendation requires a little CSS experience. You should instead create a div element that is fixed positionally.
我的第一条建议需要一点 CSS 经验。您应该创建一个位置固定的 div 元素。
Otherwise you could use the confirm() method.
否则,您可以使用 confirm() 方法。
confirm("Successful Message");
window.location.reload();
However, this will add a cancel button. Because the confirm method is not within an if statement though, the cancel button will still refresh the page like you want it.
但是,这将添加一个取消按钮。因为confirm 方法不在if 语句中,所以取消按钮仍会按照您的需要刷新页面。