Gaurav
Gaurav

Reputation: 8487

javascript iframe contents loading issue?

I have one Iframe in my website.I initialized it src at document.ready event. I placed iframe inside the jquery UI dialog, now every time when we open the dialog, Iframe load its contents, but i want that this loading process happens only once at document.ready.Is this possible?

Upvotes: 0

Views: 100

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382092

Yes, you can fetch once the content using an XmlHttpRequest, store it into a local variable and reuse this variable when you open the dialog.

var myReusableContent;

$(window).ready(function() {
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function() {
        if (httpRequest.readyState === 4) {
            if (httpRequest.status === 200) {
                myReusableContent = httpRequest.responseText;
            }
        }
    };
    httpRequest.open('GET', muurl);
    httpRequest.send();
});

When you need to fill the iframe, simply do

$('myIframe').html(myReusableContent);

Upvotes: 1

Related Questions