如何在子窗口中获取父窗口标签的html?

时间:2017-09-10 10:04:40

标签: javascript html

我通过以下代码从父窗口打开子窗口:

<html>
<head>

</head>

<script type="text/javascript">
    function foo(){ 
     window.open("child.html","child window");
}
</script>

<body id="data">

<img src = "2.jpg">
<button id="button1" name="NiceButton" onclick="foo()">click here</button>


</body>
</html>

在子窗口中,我希望得到父窗口body标签的html。所以我使用以下代码:

        <html>
<head>

</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
 <script type="text/javascript" language="javascript" src="html2canvas.min.js"></script>
<script type="text/javascript" language="javascript" src="html2canvas.js"></script>

<script type="text/javascript">

    function capture() {
            window.alert('test');

            html2canvas(window.opener.$("body")
            ,{
                onrendered: function (canvas) {                     
                            var imgString = canvas.toDataURL("image/png");
                            window.open(imgString);  

                }
            }
        );
    }
</script>

<body id="d" onload="capture()">

 <button id="button1" name="NiceButton" onclick="capture()">click here</button>

</body>
</html>

但是我收到以下错误:

TypeError: window.opener.$ is not a function

如何解决此错误?

我需要什么: 我需要获取父窗体标签的innerhtml。以下代码是否有效?

alert($("window.opener.body")) ; 

1 个答案:

答案 0 :(得分:1)

替换你的捕捉功能
function capture() {
        window.alert('test');

        html2canvas(window.opener.document.body
        ,{
            onrendered: function (canvas) {                     
                        var imgString = canvas.toDataURL("image/png");
                        window.open(imgString);  

            }
        }
    );
}