How to refresh page with JavaScript

To refresh page in JavaScript, we use location.reload() function.

It is used to reload the current document. But if the origin of the script calling location.reload() differs from the origin of the page that owns the Location object, the reload may be blocked and a SECURITY_ERROR DOMException thrown.

In the following example, I tried sending data to a PHP file via AJAX. If the total items are too many, I prefer refreshing the page rather than modifying only certain HTML DOM.

jQuery('.btn-save').on('click', function(){
	var keyword = jQuery('.keyword').val();
 	jQuery.ajax({
        url: "action.php",
        type: "GET",
        data: 'keyword=' + keyword,
        success: function (response) {
        	if(response.count > 100){
        		location.reload();
        	} else {
        		//loop response.items
        	}
        },
        error: function(jqXHR, textStatus, errorThrown) {
           console.log(textStatus, errorThrown);
        }
    });		

});

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close