user3150584
user3150584

Reputation: 3

update javascript function

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.

Upvotes: 0

Views: 1031

Answers (3)

loxxy
loxxy

Reputation: 13151

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";
}

Upvotes: 1

norlesh
norlesh

Reputation: 1861

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.

Upvotes: 1

Akshat Singhal
Akshat Singhal

Reputation: 1801

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.

Upvotes: 0

Related Questions