简体   繁体   中英

Extract links from iframe - Javascript

I am trying to extract the links in from a page loaded into Iframe that is not of the same origin as the main web page. The javascript console for FF and Chromium doesn't report any errors. Both browsers also do not extract the links from the webpage in the iframe.

HTTP/1.0 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: Origin
X-Frame-Options: ALLOW-FROM https://example.com/
Content-Type: text/html; charset=UTF-8


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
 <iframe name='iframe' id='iframe' src="https://www.example.com"></iframe>

 <script>
  var a = document.getElementById('iframe')
  var links = a.getElementsByTagName('a');

  document.write(links.length);
  for(i=0; i<links.length; i++) {
    document.write(links[i].href);
  } 
</script>
<div id="results">results</div>

Cannot be done because security.

You don't get errors because you query on the iframe element and not its contentDocument .

 <iframe name='iframe' id='iframe' src="https://www.example.com"></iframe> <script> var a = document.getElementById('iframe'); var links = a.contentDocument.getElementsByTagName('a'); </script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM