How to refresh <iframe> using ajax?

Member

by dana , in category: HTML & CSS , a month ago

How to refresh <iframe> using ajax?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , a month 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.