Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access the DOM inside an opened javascript window?

Tags:

javascript

With javascript, I can open and close a popup window as follows.

var myWindow = window.open('http://example.org/');
// do things to window.
myWindow.close();

How do I manipulate the DOM elements of the page in the popup window before closing it? I have tried

myWindow.onready = function() {
    myWindow.document.getElementById('someElementID').style.color = '#f00';
}

to no avail.

like image 255
dangerChihuahua007 Avatar asked Dec 20 '25 23:12

dangerChihuahua007


2 Answers

There is no event to let you know that the URL was loaded properly in the popup window. Only viable option is to set a timeout and then access the elements.

Like this:

var myWindow = window.open('./t6.html');  

setTimeout(check, 3000);

console.log($('body', myWindow.document)); // This shows a blank "body"

function check() {
    console.log($('body', myWindow.document)); // This shows the actual elements in the document
}

myWindow.close();
like image 138
ATOzTOA Avatar answered Dec 22 '25 12:12

ATOzTOA


Here's a fiddle that demonstrates it working without your ready handler.

var myWindow = window.open('http://example.org/');
alert(myWindow)
// Alerts an HTMLDocument object

Like @jfriend00 mentioned in the comments, there's no ready event handler for a Window object.

like image 20
Rob Hruska Avatar answered Dec 22 '25 14:12

Rob Hruska



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!