开发者

HTML: How to un-include the included .js file on current page

开发者 https://www.devze.com 2023-02-09 08:10 出处:网络
I have an Html file in which i have include a .js file like <script src=\"myCode.js\"></script>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消