0

This should be a simple problem, I just can't seem to stumble upon the right answer:

So I have a site in HTML with many pages that all link to the newest one, so I created a simple JavaScript function in a separate file:

function newest() {
    window.location = "http://xxxxxxxxxx.xxx/6.html";
}

With the line:

 < script type="text/javascript" src="javascript.js">< /script>

In my HTML document.

So I can update the number every time a new page is posted. The problem is that when I post a new one, the code doesn't refresh from the user side until you delete the cookies (if I replace it with 7, it will still redirect to 6).

Sorry if it is a stupid question, but everything I have looked up seems way off topic.

muneebShabbir
  • 2,500
  • 4
  • 29
  • 46
  • Did you title your file `javascript.js`? – tjons Jan 01 '14 at 04:13
  • Yes, it seems to work fine initially, but doesn't modify due to some issue with cookies overwriting it or something (I'm not very knowledgable with that sort of thing at all) – user3150584 Jan 01 '14 at 04:26

3 Answers3

1

The cache expects your javascript to me immutable so unless you can include the file name external to your javascript then this path is not going to work... How about just creating a 'latest.html' page that is either a file system link to the original or else redirects to the latest version.

norlesh
  • 1,749
  • 11
  • 23
  • Ended up going with this method with this code in the head of my redirection page (seems to work fine, thank you.) (Not sure how to format on this site, sorry): < meta http-equiv="refresh" content="0"; URL="http://xxxxxxxxx.xxx/6.html" /> < script type="text/javascript"> window.location.href = "http://xxxxxxxx.xxx/6.html" < /script> – user3150584 Jan 01 '14 at 04:51
  • No problem, for future reference to format code in a comment enclose the code in 'back ticks' (the alternate function of the ~ key) – norlesh Jan 01 '14 at 04:55
1

A simple client side solution would be to inject the script with different version attributes appended to it.

So HTML page can contain a script like :

var script = d.createElement('script');
script.type = 'text/javascript';
script.src = 'http://xxxxxxxxxx.xxx/javascript.js?v=' + Math.random();
d.getElementsByTagName('head')[0].appendChild(script);

Notice the random number?

where javascript.js is the one having your code:

function newest() {
    window.location = "http://xxxxxxxxxx.xxx/6.html";
}
loxxy
  • 12,990
  • 2
  • 25
  • 56
0

You can turn off the caching of the resources (javascript files) on the client machine by adding the instructions in your code for the web browser, not to cache. Refer to this link for how to turn off caching for your webpage.

Community
  • 1
  • 1
Akshat Singhal
  • 1,801
  • 19
  • 20