javascript - How to remove and add js file completely dynamicaly after page load by jquery? -


in project ,i have load js , css file dynamically after page load. using below code able add , remove js file after adding new file removing previous one,then previous file not shown in header when run code both js file code executed.i don't known how remove file ,can body me. code is.

in the view page header:-

<link rel="stylesheet" id="videocss" /> <script id="videojs"></script> 

jquery dynamically load js , css file:-

function addremovejscssfile(filename, filetype)     {         var fileref='';         if (filetype == "js") { //if filename external javascript file             document.getelementbyid("videojs").remove();             fileref=document.createelement('script');             fileref.setattribute("type", "text/javascript");             fileref.setattribute("src", filename);             fileref.setattribute("id", "videojs");         }         else if (filetype == "css") { //if filename external css file              document.getelementbyid("videocss").remove();             fileref = document.createelement("link");             fileref.setattribute("rel", "stylesheet");             fileref.setattribute("type", "text/css");             fileref.setattribute("href", filename);             fileref.setattribute("id", "videocss");         }         if (typeof fileref != "undefined")             document.getelementsbytagname("head")[0].appendchild(fileref)     } 

in function have pass file location , type of file i.e css or js add file dynamically.thank you.

i don't know if i've understood issue, let me try explain:

if original script in head section, executed immediately, because javascript procedural language, there's nothing can in script placed "later": scripts defined earlier executed earlier so, maybe problem when call function, it's late stop videojs execution.

on other side scripts defined before, can't see script tags defined later.. or better, if you're deferring, in deferred situation you'll fighting again same issue, videojs script executed (it late or early, because statements executes or before, or after, there isn't third way).

of course can load script before set global object videojs "stop, don't proceed stuff!" shall edit videojs code either, wrapper "if" asks object permission proceed, , don't think that's response you're looking for.

but question is: what's executed in javascript videojs, event binder starts on load? or in event? or sequence of statements executed - sorry wordplay - sequentially?

in first , second case there's can before: if video.js starts when event triggered, can stop function executed, operating before trigger: must delete event subscription. unbind event, must know how first script operates , event subscribes in order unbind it.

here how execute script before jquery ready event (but it's valid native javascript events, think might helpful):

running jquery code before dom ready

this answer useful every document events, not load or "jquery ready".

but, if it's possible remove bind "vanishing" every videojs script effects, it's not clean job, because

  1. overall, videojs has been executed
  2. this work videojs , @ least may work hardcoded scripts uses same event start, not hardcoded scripts

so, suggest purpose, it's manage load , unload of scripts entirely manager, don't hardcode them in html.

ps: have ever heard require.js?

if not, take here


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -