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

笑着哭i 提交于 2019-12-02 03:29:22

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

Notice the myContent variable contains the full html structure, including DOCTYPE, <html>, <head> an <body> sections. Although it's not mandatory, it will give you more control over the settings of your initial (iframed) page.

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

Refs :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!