如何使用Jquery将当前文档克隆到IFrame
任务:
当我的主页加载时,我想正常加载页面。在所有脚本运行之后,我想使用Jquery将IFrame注入到正文中,并使用它来克隆当前页面。 (为什么?所以我可以让页面的一部分不在我网站的页面之间重新加载。)
我可以使用src属性注入iframe并成功克隆页面,但是,这会导致页面上的所有内容下载两次(一次是第一次加载页面时,一次是脚本注入iframe时)。
所以,我想使用JQuery克隆当前页面(头部和主体),并将其附加到Iframe中,或使用新的html5 srcdoc属性。
目前,我对这两条路线都没有兴趣。我在这里概述两种方法:
克隆并追加
// Get the width & height of current window
wwidth = $(window).width();
wheight = $(window).height();
// inject the iframe
$("body")
.append(
'<iframe seamless ' +
' class="hidden" id="iframe" border="0" frameborder="0" width="'
+ wwidth + '" height="' + wheight + '"></iframe>'
+ '<div id="temp"></div>'
);
// Attempt to replace iframe contents with current page contents
$('iframe').contents().replace( $("html").clone().not("iframe") );
这会注入iframe但不会替换内容。
使用SRCDOC
// Same as above bu this part
// Attempt to set iframe contents with srcdoc
$('iframe').attr("srcdoc" , $('html').clone() );
生成结果srcdoc =“[object] [Object]”
附:我试图添加srcdoc标签,但我不能。请高点的人吗?
没有找到相关结果
已邀请:
2 个回复
超可林
)。 此外:克隆不会阻止在大多数浏览器中“双重下载”。
赣借
属性。 即便是这样的简单示例也不起作用: