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?

marked as duplicate by schroeder, Xander, TildalWave, RoraΖ, Jens Erat Jan 28 '15 at 21:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.