1

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?

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. Commented Jul 28, 2016 at 13:27

2 Answers 2

5

You 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.com and news.google.com are different)
  • Both windows have same port (google.com:80 and google.com:443 are different)
Sign up to request clarification or add additional context in comments.

Comments

3

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.

4 Comments

I somehow succeeded in having access to document with gmail.com when I was logged in.
@user31782: I suspect that was observation error (for instance, perhaps you were working in the console of a gmail.com page?).
No. If just open a new blank tab in firefox and open gmail with window.open I can access it's document object. May be my new tab is associated with google server?
@user31782: If you mean from the console, the console is special.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.