Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change iframe content jquery

I'd like to change my iframe content but I don't understand how

<html>
    <head>
        <title>test</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    </head>
    <body>
        <iframe id="frame" width="100%" height="95%" src="http://google.com"></iframe>
    <script type=text/javascript >
        $('#frame').contents().find( "body" ).html('<p>hi</p>')
    </script>
    </body>

</html>

With this code I always have my google page and not a ifram with hi

like image 416
Ajouve Avatar asked Dec 25 '22 11:12

Ajouve


1 Answers

<script type="text/javascript">
  $(document).ready(function(){        
       $('#frame').on('load', function() {
           $(this).contents().find( "body" ).html('<p>hi</p>');
       });
  });
</script>

While this is right, the content will never change as you are trying to modify the body of an external resource. You cannot do this due to cross-site scripting rules.

EDIT:

The only way to do it is to do what @user1153551 did and replace the whole document.

like image 176
Michael Coxon Avatar answered Jan 12 '23 08:01

Michael Coxon