Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 解析网站';在<;iframe>;_Javascript - Fatal编程技术网

Javascript 解析网站';在<;iframe>;

Javascript 解析网站';在<;iframe>;,javascript,Javascript,是否可以解析iframe中显示的html网页中的标记?我试着按如下方式编译代码,但它不起作用 <!DOCTYPE html> <html> <body> <iframe id="Frame" src="Url"></iframe> <button onclick="myFunction()">Parse it</button>

是否可以解析iframe中显示的html网页中的标记?我试着按如下方式编译代码,但它不起作用

       <!DOCTYPE html>
       <html>
       <body>

       <iframe id="Frame" src="Url"></iframe>

       <button onclick="myFunction()">Parse it</button>

      <p id="demo"></p>

      <script>
      function myFunction() {
      var x = document.getElementById("Frame").src.getElementByTagName("title");
     document.getElementById("demo").innerHTML = x;
     }
     </script>

     </body>
     </html>

解析它

函数myFunction(){ var x=document.getElementById(“框架”).src.getElementByTagName(“标题”); document.getElementById(“demo”).innerHTML=x; }
您可以执行以下操作。请记住将iframe的src更改为实际加载页面的url:

<!DOCTYPE html>
<html>
<body>


<iframe id="Frame" src="sample.html"></iframe>

<button onclick="myFunction()">Parse it</button>

<p id="demo"></p>

<script>

function myFunction() {

var x;         

//get the iframe object
var iframe = document.getElementById('Frame');

//cross browser solution to get the iframe document
//of the iframe
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;

     //if the document is not undefined
     if (iframeDocument) {

       //do something with the frames dom

       //get the content of the title tag from the iframe
       x = iframeDocument.getElementsByTagName("title")[0].innerHTML;

        }    

              document.getElementById("demo").innerHTML = x;    

}

</script>

</body>
</html>
这是另一个错误,您可能会收到同样的原因。我在自己的网站(bytewarestudios.com)上尝试了这个错误


检查此方法
document.getElementById(“框架”).contentWindow.document.getElementByTagName(“标题”)
@dandavis可以
document.title
替换
document.getElementsByTagName(“ti”)‌​tle“
?“元素”处可能缺少“s”。这是另一个我忘了反复强调的问题。好眼力。代码将显示的不是文档的标题,而是DOM对象的字符串版本,这很恶心。非常感谢您的解决方案!如果我将iframe中的src更改为URL(如www.google.com),它是否也起作用?@jiten2015不,它在google.com上不起作用,您将得到我在底部添加到解决方案的结果。很多时候,iframe不会加载url,因为服务器不允许在来自不同域的iframe中加载页面。换句话说,您尝试加载的网页与您的网页不在同一文件夹中。明白了。再次感谢您的详细解释@没问题,我很高兴能帮上忙。
Load denied by X-Frame-Options: https://www.google.com/?gws_rd=ssl does not permit cross-origin framing.
Error: Permission denied to access property "document"