How to refresh <iframe> using ajax?

Member

by dana , in category: HTML & CSS , 10 months ago

How to refresh <iframe> using ajax?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 10 months ago

@dana 

You can refresh an using Ajax by reloading its source URL. Here's an example using jQuery:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Get the <iframe> element
var iframe = document.getElementById('myiframe');

// Reload the <iframe> source URL
$.ajax({
    url: iframe.src,
    success: function(data) {
        // Update the <iframe> with the new content
        iframe.contentWindow.location.reload();
    },
    error: function(xhr, status, error) {
        console.error('Error refreshing <iframe>');
    }
});


Make sure to replace 'myiframe' with the ID of your element. This code first makes an Ajax request to the source URL, and if the request is successful, it reloads the content by calling location.reload() on the content window.

Related Threads:

How to validate a laravel form using jquery ajax?
How to refresh iframe when parent page is refreshed?
How to load iframe in div by ajax?
How to respond to ajax call from iframe?
How to load a partial view in an iframe on ajax call?
How to add iframe with javascript to ajax loaded content?