简体   繁体   中英

How to dynamically load a javascript file avoiding cache?

I want to load js files dinamically with different names avoiding cache. Now I am doing with PHP but I cant use it in a new project.

<?php echo '<script src="lib/jquery/jquery.min.js?v='.round(microtime(true)).'"></script>';?> 

This works! I create the script object with HTML. Then I can dynamically assign the name and a version timestamp parameter.

<script id="myScript"></script>
<script>     
  var microTime = Date.now();   
  var myVar = "myJsFile";
  var myUrl = myVar+'.js' + '?v=' + microTime;

  myScript.src = myUrl;
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM