简体   繁体   中英

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.

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:

<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:

<script type="text/javascript">

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

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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