JavaScript iframe内容加载问题



我的网站上有一个Iframe。我在document.ready event src初始化了它。我iframe放在jquery UI对话框中,现在每次打开对话框时,Iframe 都会加载其内容,但我希望此加载过程仅在document.ready发生一次。这可能吗?

是的,您可以使用 XmlHttpRequest 获取内容一次,将其存储到局部变量中,并在打开对话框时重用此变量。

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();
});

当您需要填充 iframe 时,只需执行

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

最新更新