0

I like to download the html code via JavaScript. This HTML-Code is an extern webpage which will be displayed in iframe. Is it possible to get HTML-Code how it is displayed in this iframe.

So here is my try:

<body>
    <iframe id="iframe_htmlcode1" name="iframe_htmlcode" src="http://www.w3schools.com"></iframe>
<textarea id="TextArea1" name="S1"></textarea>

    <script>
        function getAndSetContent() {

            var x = document.getElementById("iframe_htmlcode1");
            var y = (x.contentWindow || x.contentDocument);

            document.getElementById("TextArea1").value = y.documentElement.innerHTML;


        }
</script>
<input id="Button1" type="button" value="button" onclick="getAndSetContent()" />

But y.documentElement.innerHTML returns nothing. I've tried it also with a local html document which will be used in the iframe, which works fine.

Any suggestions?

THANKS

Kinimod
  • 166
  • 1
  • 15
  • As long as I know, it is not possible. You will get cross domain exception because of security measure. You must send your request to php, php will get that html and return it back to you, that will works. –  Jun 30 '16 at 15:27
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript – Teemu Jun 30 '16 at 15:27

1 Answers1

0

I assume that your code is working with the local file because the protocols are matching, anyway the debug says it all (you will need some basic server and then try again).

Uncaught SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "http://www.w3schools.com".  The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "http". Protocols must match.

Once you get your code on the server you may need to use something like the below to get around the cors issues

https://github.com/padolsey-archive/jquery.fn/tree/master/cross-domain-ajax

Vitaliy Terziev
  • 6,429
  • 3
  • 17
  • 25