防止加载隐藏 iframe 的内容 [英] Prevent loading the content of a hidden iframe
问题描述
我有许多隐藏的 <div>
标签,带有 (display: none)
,当我想加载 jQuery 模式时我会调用这些标签.这些 <div>
标签中的每一个都包含一个调用不同页面的 <iframe>
.
I have many hidden <div>
tags, with (display: none)
, that I call upon when I want to load a jQuery modal.
Each of these <div>
tags contain an <iframe>
which calls a different page.
为了不让我的页面加载缓慢,我想知道是否有办法阻止 <iframe>
加载页面,直到我调用 <div>
在模态里?
In order to not make my page load painfully slowly, I am wondering if there is a way to prevent the <iframe>
from loading the page, until I call the <div>
in the modal?
推荐答案
可以通过在 iframe 的 html 中给出空的 src 并稍后通过 jquery/javascript 分配 src 来加载 html 渲染后的 iframe.
You can load the iframes after html being rendered by giving empty src in html of iframe and later assigning src by jquery / javascript.
HTML
<iframe id="iframe1" ></iframe>
Javascript,iframe 可以在按钮点击等动作上加载
document.getElementById('iframe1').src="/default.aspx";
正如kern3l所说,我们可以在iframe中添加data属性来保存src而不是硬编码.
As kern3l said, we can add data attribute in iframe for holding src instead of hard coding.
HTML
<iframe id="iframe1" data-frameSrc="/default.aspx"></iframe>
Javascript
ifrmame1 = $('#iframe1')
ifrmam1.src = ifrmam1.data("frameSrc");
你也可以在 jquery 中新建一个框架并分配 src,这将加载空白框架的页面.
You can also make a new frame in jquery and assign src, this will load the page with blank frame.
$('<iframe>', {
src: '/default.aspx',
id: 'myFrame',
frameborder: 0,
scrolling: 'no'
}).appendTo('#parentDivId');
或
var iframe = document.createElement('iframe');
iframe.frameBorder=0;
iframe.width="300px";
iframe.height="250px";
iframe.id="myFrame";
iframe.setAttribute("src", '/default.aspx');
document.getElementById("parentDivId").appendChild(iframe);
这篇关于防止加载隐藏 iframe 的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!