Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html> <head> <title>Title of the Document</title> </head> <body> <iframe id="iframe" src="https://example.com"></iframe> <script> iframe.onload = function() { // get the reference to the inner window let iframeWindow = iframe.contentWindow; // OK try { //but not to the document inside it let doc = iframe.contentDocument; // ERROR } catch(err) { alert(err); // Security Error, another origin } // also we cannot READ the page URL in the iframe try { // Cannot read URL from Location object let href = iframe.contentWindow.location.href; // ERROR } catch(err) { alert(err); // Security Error } //can be WRITE to a location (and therefore load something else in the iframe) iframe.contentWindow.location = '/'; // OK iframe.onload = null; // clear the handler so that it does not start after changing the location }; </script> </body> </html>