jQuery refresh page on browser resize

Share this article

Basic code snippet to refresh the page on a browser resize using JavaScript.

//refresh page on browser resize
$(window).bind('resize', function(e)
{
  console.log('window resized..');
  this.location.reload(false); /* false to get page from cache */
  /* true to fetch page from server */
});
If window.location.reload() not working in Firefox then try the following.
//refresh page on browser resize
$(window).bind('resize', function(e)
{
  if (window.RT) clearTimeout(window.RT);
  window.RT = setTimeout(function()
  {
    this.location.reload(false); /* false to get page from cache */
  }, 200);
});

Frequently Asked Questions (FAQs) about jQuery Page Refresh

How can I refresh a page using jQuery when the browser is resized?

To refresh a page using jQuery when the browser is resized, you can use the $(window).resize() method. This method triggers the resize event or attaches a function to run when a resize event occurs. Here is a simple code snippet that demonstrates this:

$(window).resize(function() {
location.reload();
});
In this code, $(window).resize() is the event that gets triggered when the browser window is resized. The location.reload() function is used to refresh the page.

What is the role of location.reload() in jQuery?

The location.reload() function in jQuery is used to refresh or reload the page. It is equivalent to the refresh button in your browser. When this function is executed, it forces the browser to refresh the current page. The location.reload() function can be very useful when you want to reload the current document, which can be necessary in various scenarios, such as updating content dynamically or refreshing a page when the browser is resized.

Can I refresh a page without using jQuery?

Yes, you can refresh a page without using jQuery. JavaScript provides a native method for refreshing a page, which is window.location.reload(). This method works similarly to the location.reload() function in jQuery. Here is an example:

window.location.reload();
When this line of code is executed, the current page will be refreshed.

How can I refresh a page automatically after a certain time interval using jQuery?

To refresh a page automatically after a certain time interval, you can use the setTimeout() function in combination with the location.reload() function. Here is an example:

setTimeout(function(){
location.reload();
}, 5000);
In this code, the setTimeout() function will wait for 5000 milliseconds (or 5 seconds), and then it will execute the location.reload() function to refresh the page.

Is there a way to refresh only a part of the page with jQuery?

Yes, you can refresh only a part of the page with jQuery. This is often done using AJAX (Asynchronous JavaScript and XML). With AJAX, you can update a part of a web page without reloading the whole page. Here is a basic example:

$("#div1").load("demo_test.txt");
In this code, the load() function is used to load data from the “demo_test.txt” file into the “div1” element.

How can I stop a page from refreshing with jQuery?

To stop a page from refreshing with jQuery, you can use the event.preventDefault() method. This method prevents the default action of an event from happening. For example, if you have a form submission event that refreshes the page, you can prevent this from happening like this:

$("form").submit(function(event){
event.preventDefault();
});
In this code, the event.preventDefault() method stops the form from being submitted, which in turn prevents the page from being refreshed.

Can I use jQuery to refresh a page when a button is clicked?

Yes, you can use jQuery to refresh a page when a button is clicked. You can do this by attaching a click event to the button and then using the location.reload() function to refresh the page. Here is an example:

$("#myButton").click(function(){
location.reload();
});
In this code, when the button with the id “myButton” is clicked, the page will be refreshed.

How can I refresh a page with jQuery without cache?

To refresh a page with jQuery without cache, you can use the location.reload(true) function. The true parameter inside the reload() function forces the browser to reload the page from the server, bypassing the cache. Here is an example:

location.reload(true);
When this line of code is executed, the current page will be refreshed from the server, not from the cache.

Can I use jQuery to refresh a page when a specific event occurs?

Yes, you can use jQuery to refresh a page when a specific event occurs. jQuery provides various event methods, such as click(), dblclick(), keydown(), keyup(), mousedown(), mouseup(), hover(), scroll(), and many more. You can attach these events to any HTML element and then use the location.reload() function to refresh the page when these events occur.

How can I refresh a page with jQuery in a specific time interval?

To refresh a page with jQuery in a specific time interval, you can use the setInterval() function. This function repeatedly executes a function after a specified number of milliseconds. Here is an example:

setInterval(function(){
location.reload();
}, 30000);
In this code, the setInterval() function will refresh the page every 30000 milliseconds (or 30 seconds).

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQueryjs resize window refresh page
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week