1

I would like to open a new window using a url, grab its html as a string and display it in the console of the original window.

From How to get element and html from window.open js function with jquery , I tried:

console.log('response', url)
    var popup = window.open(url, '_blank', 'width=500,height=500');

popup.onload = function() {
    setTimeout(function(){ console.log( 'hi',popup.document.documentElement.outerHTML) }, 2000);
}

The popup window is created, but I don't see the popup's html in the original window's console.

What am I doing wrong?

2
  • Is the given url of the same domain where the popup is launched? Which browser are you working with? Commented Jul 2, 2015 at 1:07
  • The url is stackoverflow.com, I'm working with firefox Commented Jul 2, 2015 at 13:59

1 Answer 1

1

If it's not on the same domain, I'm pretty sure it isn't possible as it's not possible with an iframe: Get HTML inside iframe using jQuery.

However, if you own both pages, you should be able to use what is described in the first answer to the SO question you linked to (How to get element and html from window.open js function with jquery)

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.

1 Comment

If you try the fiddle on the question you linked to, and change the URL to stackoverflow.com , you'll see that it doesn't work. jsfiddle.net/JRqTy/394

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.