3

Let's assume that we have 2 open tabs in a browser containing page A (A.html, A.js) into the first tab and page B (B.html, B.js) into the second tab and that page A and B have the same origin (scheme, domain, port).

  1. Can A.js call a function from B.js?
  2. Can A.js modify the DOM of page B.html?
westbeam87
  • 419
  • 4
  • 11

1 Answers1

4

Can A.js call a function from B.js?

No, the javascript need to be defined on your current page

Can A.js modify the DOM of page B.html?

Yes, but you need a reference AND have the same origin.

If page A have a reference to page B, it is possible for A to modify the DOM in B.

Page A can have a reference to page B if page A opened page B and saved the reference. Also page B can access page A via the opener property. You can open a new tab/page in javascript with the following code :

// Open a new window
var myWindow = window.open("", "myWindow", "width=200, height=100");

// Write some text in the new window
myWindow.document.write("<p>This is 'myWindow'</p>");     

// Write some text in the window that created the new window            
myWindow.opener.document.write("<p>This is the source window!</p>");

Same Origin Policy Limitation

The same origin policy limit what you can access. If the 2 pages are on different domains, you will not be able to access the window.document property to read or write. Here is a list of what you can access when the other page is from a different domain.

Gudradain
  • 6,921
  • 2
  • 26
  • 43