0

If I had the following <script> tag in my code:

<script src='http://your.site.com/js/big_script.js?version=1'></script>

How can I get this version number to automatically update with each subsequent window.reload() action placed on that page?

Page refreshes...

<script src='http://your.site.com/js/big_script.js?version=2'></script>

Page refreshes...

<script src='http://your.site.com/js/big_script.js?version=3'></script>

and so on... through use of JavaScript only?

I know localStorage would come in handy here but implementing it within the <script> tag itself? Is there a way of doing this, or are there alternative ways it could be done?

In this case I'm only after JavaScript based solutions only as the application is pure HTML5/JavaScript/CSS3.

Thanks in advance!

Eliseo D'Annunzio
  • 592
  • 1
  • 10
  • 26
  • Do you want to increment the counter if the user opens another browser tab to the same URL? – Femi Mar 20 '14 at 03:28
  • @MjrKusanagi This application will not be run in a standard browser. It will be run in kiosk mode with no access to a keyboard on a touchscreen interface. – Eliseo D'Annunzio Mar 20 '14 at 04:15

1 Answers1

2

As you noted, localStorage is likely the way to go. The downside of doing this client side is that you'll have to late bind your script tag. I would try:

var my_site_cntr = parseInt(localStorage.getItem('mySite.counter')||0);
my_site_cntr++;
localStorage.setItem('mySite.counter', my_site_cntr);
var scrptElem = document.createElement("script");
    scrptElem.type = "text/javascript";
    scrptElem.src = "http://your.site.com/js/big_script.js?version=" + my_site_cntr;
    scrptElem.innerHTML = null;
document.body.appendChild(scrptElem);

Sources:

Community
  • 1
  • 1
Femi
  • 1,332
  • 9
  • 20
  • Thanks for that... Will the new ` – Eliseo D'Annunzio Mar 20 '14 at 22:42
  • 1
    It will be appended to the body element because I used the selector `document.body`. You can append it to any other element you want by changing `document.body` to something like `document.getElementById('id')`. You can read more about this and and other JS DOM selectors here: http://www.w3schools.com/jsref/met_doc_getelementbyid.asp – Femi Mar 20 '14 at 22:58