11

I am trying to open a popup like this:

$('#btn').click(function () {
    var popup = window.open('mypage.php', '_blank', 'width=500,height=500');
    var dom = popup.document.body;
    for (i in dom) {
        console.log(dom[i]);
    }
});

Now what I want to do is to get the html from the popup window and also be able to use maybe a jQuery function from the window.opener (the page that opened the popup)

PS. In the console there are a lot of things printed but no html source.

Use this to try: http://jsfiddle.net/JRqTy/

Thanks in advance.

1
  • Did you solve this? The solutions below are not working for me. I'm using React, but I don't think that's the problem. Commented Jan 20, 2023 at 14:50

2 Answers 2

6

Try:

var html = popup.document.documentElement.outerHTML

EDIT

The window is not loaded immediately. Something like this will work, assuming that you're not attempting to violate the same-origin policy:

$('#btn').click(function() {
    var popup = window.open('[Your URL HERE]', '_blank', 'width=500,height=500');
    
    popup.onload = function() {
        setTimeout(function(){ console.log(popup.document.documentElement.outerHTML) }, 2000);
    }  
});​

Here's a working fiddle.

Note: If you control both the parent and the child source, you could also have the child invoke a method on the parent that passes in it's html:

Child Window

// Call when body is loaded
function SendHtmlToParent() {
    window.opener.HtmlReceiver(document.outerHTML);
}

Parent

function HtmlReceiver(html) {
    console.log(html);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Actually, the fiddle example is not working, nor can I get it working myself. I've spent several hours and cannot get windows or iframes working with access by the parent/opener. Firefox 63.0.3 (64bit)
@DavidSpector, are you violating the same origin policy? Are the parent and child window on the same domain, or are both of the domains under your control with a valid same origin policy?
They are not only in the same domain but in the same directory. I'm working on a tool to help design websites and it never has to work across different domains. It does have to have a window or pane showing the page under development and be able to manipulate its elements. Thanks for asking. Getting help is hard enough without getting accusations.
Please update your same-origin policy link - it's dead.
1

Wait...

Note that remote URLs won't load immediately. When window.open() returns, the window always contains about:blank. The actual fetching of the URL is deferred and starts after the current script block finishes executing. The window creation and the loading of the referenced resource are done asynchronously.

MDN

And if you wait, you will get this error:

Unsafe JavaScript attempt to access frame with URL https://www.google.co.il/ from frame with URL http://fiddle.jshell.net/_display/. Domains, protocols and ports must match.

So it can be done with google, unless you're working there...

Fiddle

If you open something "valid" it will work fine.

Working DEMO

3 Comments

@OscarJara. We cleaned all the thread from comments. it was a mess.
This fiddle "working demo" is also not working, 6 years later.
I think waiting for the new window or iframe to load is key to making this work, and none of the Mozilla or other tutorials I can find mention this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.