读取iframe内容html

时间:2018-08-14 09:23:41

标签: javascript jquery html iframe

在我的页面中,我拥有一个iframe,该iframe的来源是一个URL,该URL具有另一个服务器的页面。

页面加载时抓取输出并将其显示在iframe中。

现在,我要使用以下代码在iframe完全加载后复制其内容-

<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">
    function getContent()
    {
        var frame = window.frames["iView"].document;
        alert(frame.getElementById('CMS').innerHTML);
    }
</script>
</head>
<body>
  <iframe src="http://www.example.com/abc.jsp" name="iView" style="width:600px;height:800px;border:dotted 1px red" frameborder="0" onload="getContent();"></iframe>
</body>
</html>

但是它不起作用,我也尝试了使用jquery的许多方法,但是在jquery中,它在加载iframe内容之前运行了jquery,这肯定是行不通的。请提出。

1 个答案:

答案 0 :(得分:1)

浏览器根据代码顺序从上到下加载页面内容。

使用jquery ready函数在文档加载后使函数可用。在这种情况下,您的代码应如下所示:

<html>
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">

    $(document).ready(function(){
        function getContent()
         {
            var frame = window.frames["iView"].document;
            alert(frame.getElementById('CMS').innerHTML);
         }

         getContent();
    });
</script>
</head>
<body>
  <iframe src="http://www.example.com/abc.jsp" name="iView" style="width:600px;height:800px;border:dotted 1px red" frameborder="0"></iframe>
</body>
</html>
相关问题