0

Here is the jsfiddle.

When I execute

$('body').append('helo')

inside iframe, the string was appended to the parent.

How can I avoid this behaviour without changing the script inside the iframe.

gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
  • possible duplicate of [Why does appending a – gongzhitaao Apr 28 '14 at 17:31

2 Answers2

0

Maybe try something like this:

http://jsfiddle.net/WZ84a/1/

but it is not easy. i have found another source here

<iframe id="demo"></iframe>

var frame = document.getElementById("demo");
var html = 'data:text/html;charset=utf-8,' +encodeURI('<body>Hello</body>');
frame.src = html;

I would personally use DIVs as it is very good and useful, can do everything an iFrame can do and more (Yes, maybe there is something that i do not know of, but nothing which I use in my everyday web development life!). iFrames is old and poor

EDIT: Just found this jsFiddle, Problem solved

Community
  • 1
  • 1
Pierre
  • 8,397
  • 4
  • 64
  • 80
  • Thanks. But what about scripts? `$('#demo').contents().find('body').append('helo');` works too. But when it's comes to script, the string is always appended to the parent. – gongzhitaao Apr 28 '14 at 16:02
  • @gongzhitaao the thing is about iFrames, you don't always have control over them for security reasons, you can however call javascript in the parent from within the iFrame. – Pierre Apr 28 '14 at 16:13
  • I still don't see the point. I want to use js to **dynamically** add content to iframe, not static content. I know how to do it in the static way as I've shown in my previous comment. – gongzhitaao Apr 28 '14 at 16:19
0

Just to help others, please refer to the following answers:

Why does appending a to a dynamically created seem to run the script in the parent page?

It works.

gongzhitaao
  • 6,566
  • 3
  • 36
  • 44