javascript - How to remove loaded script and add new script? -
i creating demo in have added query string in url. when load html value of query. want if query string has value "simple", removes js file loaded.
if finds value other "simple" should load js file.
i google searched solution, nothing works.
here html:
<!doctype html> <html> <head> <script src="js/jquery-1.11.0.min.js" type="text/javascript"></script> <script src="js/copynode.js" type="text/javascript" id="dynamic_files"></script> <script type="text/javascript"> $(window).load(function(){ // full load var query = decodeuricomponent(window.location.search.substring(1)); console.log("---------------------"); var index=query.indexof("=") query=query.substr(index+1,query.length); console.log(query); if(query=="simple"){ alert('if--'); //$('#dynamic_files').remove(); }else{ alert('else--'); $('#dynamic_files').remove(); var script = document.createelement('script'); script.src = 'js/index.js'; document.getelementsbytagname('div')[0].appendchild(script); } }); </script> </head> <body > <h1>my web page</h1> <p id="demo">a paragraph.</p> <button type="button" onclick="loadscript()">load script</button> </body> </html>
you can check value of string
first write function :
function checkstring(){ if($('#id_of_element_of_the_string').value == "simple"){ //load script needed var head = document.getelementsbytagname('head').item(0); var script = document.createelement('script'); script.setattribute('type', 'text/javascript'); script.setattribute('src', 'http://mysite/my.js'); head.appendchild(script); }else{ //load other part. } }
Comments
Post a Comment