I have a web page index.php, I want to add and remove JS files to this page dynamically
So far I did,
<script type="text/javascript" src="" id="dynamic_files"></script>
I had planned to change the src of this script tag by
function loadJsfiles( filename ){
var filePath = 'include/js/'+filename;
$("script#dynamic_files").attr('src',filePath);
};
and the included js file has script tags in it:
document.write('<script type="text/javascript" src="include/js/profile1.js"></script>');
document.write('<script type="text/javascript" src="include/js/profile2.js"></script>');
document.write('<script type="text/javascript" src="include/js/profile3.js"></script>');
document.write('<script type="text/javascript" src="include/js/profile4.js"></script>'); –
My question is that,
will that script tag printed in to my document page, if so how do I remove it
Reference: Dynamic js files
You CANNOT do document.write after load
Look here:
Javascript: Run "document.write" in createElement script failed
You will notice your page will be wiped.
It does not matter which method you use to insert scripts, you need to either stop them from using document.write OR replace the document.write with a custom method.
Perhaps you want to use something like require.js – which is a javascript loader (thanks Jared for a better link than
https://developers.google.com/loader/ )