-3

I tried like this

<script type="text/javascript" src="myfile.js"?+Date.now()></script>

In browser it is showing as it is. I want to add some timestamp to each js files. i.e.,

<script type="text/javascript" src="myfile.js"?+5671836294></script>

Thanks in advance.

Paul Abbott
  • 7,065
  • 3
  • 27
  • 45

2 Answers2

3

You can't randomly stick JavaScript anywhere you like in HTML.

When you are in the middle of an HTML start tag you can either:

  • End the tag with >
  • Write an attribute

JavaScript does not belong there.

If you want to generate an HTML attribute value dynamically when the element is created, then you must create the entire element with JavaScript.

e.g.

<script>
    var s = document.createElement("script");
    s.src = "myfile.js"?+Date.now();
    document.head.appendChild(s);
</script>

… but you'd probably be better of solving this problem by properly configuring your HTTP headers for the script instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Refresh the page or rewrite the script tag and append it to the bottom of the body.

But once a page is loaded, it is loaded. You have to redeclare to overwrite.

Christian4423
  • 1,746
  • 2
  • 15
  • 25