How to append timestamp to the javascript file in <script> tag url to avoid caching

Viewed 84148

I want to append a random number or a timestamp at the end of the javascript file source path in so that every time the page reloads it should download a fresh copy.

it should be like

<script type="text/javascript" src="/js/1.js?v=1234455"/>

How can i generate and append this number? This is a simple HTML page, so cant use any PHP or JSP related code

8 Answers

Replace regular expression if not alphanumeric Date.prototype.toISOString()

var today = new Date();
"MyFile_" + today.toISOString().replace(/[^\w]/g, "") + ".pdf"

MyFile_20191021T173426146Z.pdf

Old post but here is a one liner:

<script src="../Scripts/source/yourjsname.js?ver<%=DateTime.Now.Ticks.ToString()%>" type="text/javascript"></script>
Related