2019年8月23日 星期五

insert iframe content without document.write

Old ways:

var doc = document.getElementById('myIframe').contentWindow.document;
doc.open();
doc.write("<html>....</html>");
doc.close();
But chrome started to show the warning from 2018, therefore, you can modify as following way:

document.getElementById('myIframe')
    .contentWindow.document.documentElement.innerHTML = "<html>....</html>";

If you want to add jQuery in iframe:

var iframeWindow = document.getElementById('myIframe').contentWindow;
iframeWindow.$ = function (selector) {
    return jQuery(selector, iframeWindow.document.body);
};