Why setting a variable in a iframe works in pure Javascript way but not in Jquery? -
this question has answer here:
- can't append <script> element 16 answers
i'm trying add variable in iframe, works in javascript way not want in jquery way. here jsfiddle.
pure javascript way:
var iframedocument = $('#iframe')[0].contentwindow.document; var scriptobject = iframedocument.createelement("script"); scriptobject.type = "text/javascript"; scriptobject.innerhtml = "var variable=9;"; iframedocument.body.appendchild(scriptobject);
result okay in console (variable set , equals 9 , iframe context) :
and when try change value of variable in jquery:
$('#iframe').contents().find('body').append($('<script type="text/javascript">').html('variable=10;'));
the variable loaded in parent context , didn't change in iframe!
i know javascript way better jquery way wondering why doesn't work expected.
you missing </script>
, way, cant't inject that. try this:
$('#iframe').contents().find('body').append($('<script type="text/javascript"><\/script>').html('var variable=10;'));
Comments
Post a Comment