简体   繁体   English

Fancybox iframe,其内容在启动页面上以js编码-如何在页面加载时打开?

[英]Fancybox iframe with content encoded in js on launch page - how to open on page load?

Thanks to this answer , I have a way of launching a fancybox iframe on page-load: http://www.casedasole.it/fancybox/A.html 有了这个答案 ,我有了一种在页面加载时启动fancybox iframe的方法: http : //www.casedasole.it/fancybox/A.html

This answer shows how to open a fancybox iframe whose content is encoded in javascript in the page that launches the iframe: http://www.casedasole.it/fancybox/B.html 此答案说明了如何在启动iframe的页面中打开其内容用javascript编码的fancybox iframe: http : //www.casedasole.it/fancybox/B.html

Is there some way of having B do what A does - ie launch itself on page-load? 有什么办法让B进行A的工作-即在页面加载时启动自身?

NB: The hidden div approach ( see here ) won't work because I need to be able to navigate within the iframe once opened. 注意:隐藏的div方法( 请参阅此处 )将不起作用,因为打开后,我需要能够在iframe中进行导航。

Just do : 做就是了 :

var myContent = "..."; // html as option "B"

jQuery(document).ready(function ($) {
    $.fancybox({
        // API options as "B"
        // build the iframe
        content: '<iframe id="myFrame" class="fancybox-iframe" frameborder="0" vspace="0" hspace="0" src="about:blank"></iframe>',
        afterShow: function () {
            var oIframe = document.getElementById('myFrame');
            var iframeDoc = (oIframe.contentWindow.document || oIframe.contentDocument);
            iframeDoc.open();
            iframeDoc.write(myContent);
            iframeDoc.close();
        }
    });
}); // ready

See JSFIDDLE 参见JSFIDDLE

Notice the myContent variable contains the full html structure, including DOCTYPE , <html> , <head> an <body> sections. 注意 myContent变量包含完整的 html结构,包括DOCTYPE<html><head><body>部分。 Although it's not mandatory, it will give you more control over the settings of your initial (iframed) page. 尽管不是强制性的,但它将使您对初始(iframe)页面的设置有更多控制。

Also notice in my jsfiddle I separated the base content ( html basic structure) from the custom content (the <div> as in your example) for tidiness purposes ;) 还要注意,在我的jsfiddle中,出于整理目的,我将基本内容( html基本结构)与自定义内容(如您的示例中的<div> )分开了;)

Refs : 参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM