Search code examples
javascripthtmlembed

Access CURRENT src in embed object HTML


I have created an embed object in HTML and this code in Javascript:

function mainLoop() {
  var browser = document.getElementById("browser");
  console.log(browser.src);
}

//call main loop
setInterval(mainLoop, 1000);
<embed src="https://en.wikipedia.org/wiki/Main_Page" id=browser>

When I run the webpage, the console constantly prints "https://en.wikipedia.org/wiki/Main_Page", which is what is expected. However, when I click on a link inside the embed HTML to change the webpage inside the embed HTML, the console continues to print "https://en.wikipedia.org/wiki/Main_Page".

How do I access the current URL of the content inside of the embed HTML?

(I cannot use browser.contentWindow because of a Security Error)


Solution

  • Unfortunately due to CORS, this is impossible. Perhaps find a different way of doing this, or make a page similar to the one shown.

    You cannot access the src of an iframe that is from another domain.