window.open: is it possible open a new window with modify its DOM

我的梦境 提交于 2019-12-24 11:19:14

问题


I want open a new window:

var my = window.open('iframe.html', '_blank' ,"height=600, width=600");

but just when open it, I want modify its DOM, I have tried:

var div = my.document.createElement("div");
div.innerHTML = "Hello!";
my.document.body.appendChild( div );

or:

var div = $('<div>Hello World</div>');
$(my).contents().find('body').append(div);

Both of them doesn't work;

So is it possible modify my iframe DOM when I open it?


回答1:


It is possible to access and modify the iframe DOM only before the child window is loaded.

var my = window.open('iframe.html', '_blank' ,"height=600, width=600");

my.onload = function () {
 var div = my.document.createElement("div");
 div.innerHTML = "Hello!";
 my.document.body.appendChild( div );
};

Also, make sure that the parent window is not closed before the child window is loaded.



来源:https://stackoverflow.com/questions/12192239/window-open-is-it-possible-open-a-new-window-with-modify-its-dom

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