Why setting a variable in a iframe works in pure Javascript way but not in Jquery? -


this question has answer here:

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

enter image description here

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! enter image description here

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

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -