简体   繁体   English

从iframe中提取链接-Javascript

[英]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. 我试图提取从加载到的iframe的页面是一样的原点作为主网页的不是链接。 The javascript console for FF and Chromium doesn't report any errors. 对于FF和铬JavaScript控制台不会报告任何错误。 Both browsers also do not extract the links from the webpage in the iframe. 这两种浏览器还没有提取在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元素,而不是其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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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