admin管理员组文章数量:1399903
I open a website in a new tab through javascript by writing the following code in the browser console:
var win = window.open(".php", "mywin", '');
Now I want to add text in a field in the newly opened tab. But for that I need the access to win.document
. When I write win.document
in the console I get the following error:
Error: Permission denied to access property "document"
This error doesn't appear if I open other websites in new tab. So,
How to get access of document object of a window opened in a new tab with window.open?
I open a website in a new tab through javascript by writing the following code in the browser console:
var win = window.open("http://staging.redefinewebs.in/wildgoose-const/wp-admin/post-new.php", "mywin", '');
Now I want to add text in a field in the newly opened tab. But for that I need the access to win.document
. When I write win.document
in the console I get the following error:
Error: Permission denied to access property "document"
This error doesn't appear if I open other websites in new tab. So,
How to get access of document object of a window opened in a new tab with window.open?
Share Improve this question asked Jul 28, 2016 at 13:23 user31782user31782 7,59716 gold badges79 silver badges157 bronze badges 1- You cannot write to a document on a remote site unless its specifically been written to allow you to do so, that page has not, so you can't. The only alternative is to proxy the page with a server side script. – Alex K. Commented Jul 28, 2016 at 13:27
2 Answers
Reset to default 5You cannot access a child window's DOM, if it violates the Same Origin Policy.
You can access the DOM of a child window, only if the following three conditions are satisfied.
- Both windows have same protocol (http/https)
- Both windows have same host (google. and news.google. are different)
- Both windows have same port (google.:80 and google.:443 are different)
How to get access of document object of a window opened in a new tab with window.open?
If the window is opening a document from a different origin, you don't; cross-origin access is denied by the browser because of the Same Origin Policy. From the error in your question, that would seem to be the case here.
If the window contains a document from the same origin, you can access it as you've shown; but note that it may still be loading immediately after you call window.open
and you might need to wait for it to finish doing so, perhaps with the DOMContentLoaded
event.
本文标签: javascriptHow to get access of document object of a window opened with windowopenStack Overflow
版权声明:本文标题:javascript - How to get access of document object of a window opened with window.open? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744237064a2596604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论