Popup window accessing parent dom

a 夏天 提交于 2019-11-29 03:13:33
Jason M. Batchelor

Sajjan's answer is a start, but better make sure your objects are available before you try to access them:

var opener = window.opener;
if(opener) {
    var oDom = opener.document;
    var elem = oDom.getElementById("your element");
    if (elem) {
        var val = elem.value;
    }
}

Otherwise, you do run the risk that the opener doesn't respond to your initial call, and that you can't get the element from it.

As jQuery, I think (based on an answer, here: how to access parent window object using jquery?):

var opener = window.opener;
if(opener) {
    var elem = opener.$("#elementId");
    if (elem) {
        var val = elem.val(); // I forgot we're dealing with a jQuery obj at this point
    }
}

window.opener.document.getElementById("your element").value

According to MDN, window.open() will return you a handle to the new window.

var popUpHandle = window.open();

With this handle you should be able to access the DOM of the PopUp. It is possible vice-versa using the already mentioned window.opener. Refer again to MDN:

var originalWindow = window.opener;

Still, your favorite search engine will provide you more details, as this is topic is fairly old and your approach has already been done a million times or more.

parent.document helped in my case.

var elem = parent.document.getElementById("overlay_modal");
if (elem) {
alert('setting attribute');
elem.setAttribute("onclick", "Windows.close('2', event);");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!