jQuery Reload an iFrame

Share this article

Simple jQuery code snippet to reload an iFrame by accessing the contentWindow property to obtain the location object. The jQuery snippet code actually loops through and refreshes all the iFrames on the page. Also see:

//reload 1 iframe
$('#iframe').contentWindow.location.reload(true);

//reload all iFrames
$('iframe').each(function() {
  this.contentWindow.location.reload(true);
});

Frequently Asked Questions (FAQs) about jQuery and iFrame Reloading

What is the basic syntax for reloading an iFrame using jQuery?

The basic syntax for reloading an iFrame using jQuery is quite simple. You just need to select the iFrame using its ID or class and then call the ‘attr’ method to set its ‘src’ attribute to its current value. Here’s a basic example:

$("#myIframe").attr('src', $("#myIframe").attr('src'));

In this example, ‘#myIframe’ is the ID of the iFrame. This line of code will effectively reload the iFrame.

Can I reload an iFrame without using its ID or class?

Yes, you can reload an iFrame without using its ID or class. You can use the ‘prop’ method to get the ‘src’ property of the iFrame and then set it to the same value. Here’s an example:

$('iframe').prop('src', function(i, val) { return val; });

This code will reload all iFrames on the page.

How can I reload an iFrame automatically at regular intervals?

You can use the ‘setInterval’ function in JavaScript to reload an iFrame at regular intervals. Here’s an example:

setInterval(function(){
$("#myIframe").attr('src', $("#myIframe").attr('src'));
}, 5000);

This code will reload the iFrame every 5 seconds.

Can I reload an iFrame when a button is clicked?

Yes, you can reload an iFrame when a button is clicked. You just need to attach a click event handler to the button and reload the iFrame in the handler. Here’s an example:

$("#myButton").click(function(){
$("#myIframe").attr('src', $("#myIframe").attr('src'));
});

In this example, ‘#myButton’ is the ID of the button.

Can I reload an iFrame when a form is submitted?

Yes, you can reload an iFrame when a form is submitted. You just need to attach a submit event handler to the form and reload the iFrame in the handler. Here’s an example:

$("#myForm").submit(function(e){
e.preventDefault();
$("#myIframe").attr('src', $("#myIframe").attr('src'));
});

In this example, ‘#myForm’ is the ID of the form.

Can I reload an iFrame when a link is clicked?

Yes, you can reload an iFrame when a link is clicked. You just need to attach a click event handler to the link and reload the iFrame in the handler. Here’s an example:

$("#myLink").click(function(e){
e.preventDefault();
$("#myIframe").attr('src', $("#myIframe").attr('src'));
});

In this example, ‘#myLink’ is the ID of the link.

Can I reload an iFrame when the page is loaded?

Yes, you can reload an iFrame when the page is loaded. You just need to attach a load event handler to the window and reload the iFrame in the handler. Here’s an example:

$(window).load(function(){
$("#myIframe").attr('src', $("#myIframe").attr('src'));
});

This code will reload the iFrame as soon as the page is loaded.

Can I reload an iFrame when the page is unloaded?

Yes, you can reload an iFrame when the page is unloaded. You just need to attach an unload event handler to the window and reload the iFrame in the handler. Here’s an example:

$(window).unload(function(){
$("#myIframe").attr('src', $("#myIframe").attr('src'));
});

This code will reload the iFrame as soon as the page is unloaded.

Can I reload an iFrame when the user scrolls the page?

Yes, you can reload an iFrame when the user scrolls the page. You just need to attach a scroll event handler to the window and reload the iFrame in the handler. Here’s an example:

$(window).scroll(function(){
$("#myIframe").attr('src', $("#myIframe").attr('src'));
});

This code will reload the iFrame whenever the user scrolls the page.

Can I reload an iFrame when the user resizes the window?

Yes, you can reload an iFrame when the user resizes the window. You just need to attach a resize event handler to the window and reload the iFrame in the handler. Here’s an example:

$(window).resize(function(){
$("#myIframe").attr('src', $("#myIframe").attr('src'));
});

This code will reload the iFrame whenever the user resizes the window.

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.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week