Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
88
rated 0 times [  91] [ 3]  / answers: 1 / hits: 23556  / 10 Years ago, mon, april 7, 2014, 12:00:00

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


More From » jquery

 Answers
31
<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.


[#71575] Saturday, April 5, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gerardob

Total Points: 571
Total Questions: 115
Total Answers: 96

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;