简体   繁体   English

在加载时将代码注入到iframe中

[英]Inject code in to an iframe on load

I want to add some html and javascript code to an iframe when it loads so that, while the user surfs, it will go to other links through an iframe. 我想在加载iframe时向其添加一些html和javascript代码,以便在用户上网时,它将通过iframe转到其他链接。

I have tried: 我努力了:

<script src="http://code.jquery.com/jquery-2.0.2.js"></script>
<body>
     <button id='button1' onclick="changecode()">Change Code</button>
     <iframe id='iframe1' name='iframe1' src='http://example.in/Default.aspx'> </iframe>
     <script type="text/javascript">
          function changecode(){
             $('#iframe1').contents().find('div.xyz').load('2.html');
          }
     </script>
</body>

Another javascript attempt: 另一个JavaScript尝试:

<script type='text/javascript'>
    function changecode() {
         document.frames('iframe1').document
            .getElementById('ContentPlaceHolder1_UpdatePanel1').innerHTML='11';
    }
</script>

The load event will not fire from an html element of a document, but you can attach a load handler from the frame itself: 加载事件不会从文档的html元素触发,但是您可以从框架本身附加加载处理程序:

<script type="text/javascript">

    function changecode(){
        $('#iframe1').load(function () {
            $(this).contents().find('div.xyz').html('11');
        });
    }

</script>

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

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