How to reload a page after the OK click on the Ale

2019-02-08 21:08发布

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

10条回答
女痞
2楼-- · 2019-02-08 21:33

Try this code..

IN PHP Code

echo "<script type='text/javascript'>".
      "alert('Success to add the task to a project.');
       location.reload;".
     "</script>";

IN Javascript

function refresh()
{
    alert("click ok to refresh  page");
    location.reload();
}
查看更多
男人必须洒脱
3楼-- · 2019-02-08 21:39

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 :

if(confirm('Successful Message')){
    window.location.reload();  
}
查看更多
迷人小祖宗
4楼-- · 2019-02-08 21:39
alert('Alert For your User!') ? "" : location.reload();

You can write above code in this format also.It seems quite decent

查看更多
Viruses.
5楼-- · 2019-02-08 21:40

As the alert method in JavaScript does not return a Boolean or yield the current thread, you must use a different method.

My number one recommendation requires a little CSS experience. You should instead create a div element that is fixed positionally.

Otherwise you could use the confirm() method.

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.

查看更多
孤傲高冷的网名
6楼-- · 2019-02-08 21:42

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:

<script type="text/javascript">
    alert('foo');
    window.location.reload(true);
    alert('bar');
    window.location.reload(true);
    alert('foobar');
    window.location.reload(true);
</script>
查看更多
Luminary・发光体
7楼-- · 2019-02-08 21:43

Bojan Milic answer work in a way but it error out with a message below,

enter image description here

This can be avoid with,

function message() 
{
alert("Successful message");
window.location = 'url_Of_Redirected_Page' // i.e. window.location='default.aspx'
}
查看更多
登录 后发表回答