[javascript] How to reload a page after the OK click on the Alert Page

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

This question is related to javascript jquery alert page-refresh

The answer is


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.

If that doesn't work then you problem might be something deeper because all the solutions already given should work.


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'
}

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();  
}

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:

if(!alert('Alert For your User!')){window.location.reload();}

I hope that this helped!? :)


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.


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>

alert('Alert For your User!') ? "" : location.reload();

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


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();
}

use confirm box instead....

    var r = confirm("Successful Message!");
    if (r == true){
      window.location.reload();
    }

Try this:

alert("Successful Message");
location.reload();

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to alert

Swift alert view with OK and Cancel: which button tapped? Bootstrap Alert Auto Close How to reload a page after the OK click on the Alert Page How to display an alert box from C# in ASP.NET? HTML - Alert Box when loading page How to show an alert box in PHP? Twitter Bootstrap alert message close and open again How to handle login pop up window using Selenium WebDriver? How to check if an alert exists using WebDriver? chrome undo the action of "prevent this page from creating additional dialogs"

Examples related to page-refresh

How to reload page the page with pagination in Angular 2? How to force reloading a page when using browser back button? How to reload a page after the OK click on the Alert Page Refresh a page using JavaScript or HTML Check if page gets reloaded or refreshed in JavaScript Force page scroll position to top at page refresh in HTML JavaScript hard refresh of current page