How to update javascript file content on the page?

Asked

Viewed 112 times

1

I searched in the forum and did not find a definitive solution, my problem is with regard to browser cache, when I make a change in a javascript file, my client only gets the updated file after cleaning the cache.

We have treated as code below:

<script src='<%=Page.ResolveClientUrl("~/Scripts/MeuJavaScript.js?sid=" + Session.SessionID) %>

Where we concatenate the user session to the file link, however, even with this the file remains outdated.

Thank you.

  • Have you tried using instead of Session.Sessionid, force some other concatenation just to see if it updates? I don’t know, just put a random number in front and see if you catch up.

  • https://msdn.microsoft.com/en-us/library/aa478965.aspx

1 answer

1


You can configure the browser through the meta tag not to cache the content of your site, this way:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

Update

You can make at the beginning of your script an ajax request to check the version on your server, so every time it updates you run the command:

window.location.reload(true)

Or send a message to the user informing about the new script.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.