Wednesday, 15 July 2015

javascript - Is it possible to use require.js on a dynamically built page with iframe? -



javascript - Is it possible to use require.js on a dynamically built page with iframe? -

so i'm working iframe in page dynamically built jquery elements. want load several external javascript files in specific order within iframe. possible load require.js in iframe , utilize load external js files through js code?

i did tests , got working this:

select iframe head

$iframe = $('#target'), $head = $iframe.contents().find("head");

add require iframe

$head.append($("<script>", {src: "/path_to/require.js"}));

for reason data-main attribute beingness ignored had turn on config myself

var startrequire = (function() {/** requirejs.config({ "baseurl": "/your/path", //more config }); // load main app module start app requirejs(["/your/path/main.js"]); **/}) .tostring().split('\n').slice(1, -1).join('\n'); //this hack here don't maintain adding plusses , "'", configs can complex $head.append($('<script>').html(startrequire))

keep in mind when adding .js parent iframe, current scope of loaded script(s) still parent if you're using jquery in main.js have declare scope:

var iframe = $('#target')[0].contentwindow.document; //iframe $('.selected', iframe); //look element class selected within iframe

javascript iframe requirejs

No comments:

Post a Comment