HTML: How to un-include the included .js file on current page
I have an Html file in which i have include a .js file like
<script src="myCode.js"></script>
what can i do to remove that file from according to any condition later? like:
if(someCondition开发者_开发知识库 == true )
{
/* some code to remove myCode.js file */
alert('myCode.js file doesnt exist on this document anymore');
}
Short answer: You can't. JavaScript files loaded like this
<script src="mycode.js"></script>
are executed as soon as the document loads. If it is possible to check the condition during the page load, you could use a document.write()
call to include the HTML that loads the script only if the condition is satisfied, or if the script can be loaded after the document has finished loading, you could use something like jQuery's $.getScript()
.
If it isn't possible either way, the best you can do is to try to undo everything that the script does, such as attempting to remove any functions, DOM elements, timeouts, and event handlers that have been added.
You could rewrite your code so that it looks like this:
if (someCondition != true)
{
document.write('<link type="text/javascript" href="myCode.js" />');
}
That way, you include the .js based on your condition.
精彩评论