1

I have a question. In old project that I'm currently working on I have found this code:

  <script type="text/javascript" language="Javascript" src='<%= Page.ResolveUrl("~/javascripts/CardConnectorManager.js?2016071203")%>'></script>

I have:

  • /javascripts/CardConnectorManager.js

but don't have

  • /javascripts/CardConnectorManager.js?2016071203

What that question mark is doing and why anybody write such thing? Maybe this file exists only on server on some bulid stuff thing?

2

4 Answers 4

4

David R's answer is pretty good, but I want to add a little bit info:

Usually there are two approaches for cache-breaking:

  1. Rename file;
  2. Add some hash to the end of the file.

The first approach may be better for some cases (see this question), but can be more painful. How would you keep this file in version control? What if there are many files like this?

The second approach is much easier. You just add something like app.js?_=<some_string>. The <some_string> can be whatever: timestamp, build number or just a random string.

For this approach, you may find it better to use automatic tools like gulp-rev.

Update: Honestly, it would be much better to have a revision number for all statics in the project: html, images, css, js. There a lot of tools to make this automatic.

Alternatively, there are some technics, for example angular developers have the $templateCache service which allows the developer to put all the project's html (excluding index.html) in a single js file.

Sign up to request clarification or add additional context in comments.

Comments

2

It refers to the same CardConnectorManager.js file.

To prevent caching, suffixing date/timestamp while calling calling a .js file is a common practice among developers.

Hope this helps!

Comments

1

Basically the JS file ending with "?" (question mark) followed by some random number is used to forcefully refresh the browser cache for that particular file. Browser's stores the downloaded js files for that website in it's cache memory, to forcefully refresh this it is suffixed with the random number.

In your example if you observe closely, the number specified is nothing but the date time stamp i.e. - the number 2016071203 represents - 2016-07-12 03. If you have updated this file on server, you just need to update the new time-stamp (you can use any random number). The time-stamp is generally used to avoid duplication of number.

So next time whenever you make changes in that JS file, just update that number, so all the clients accessing this file will get updated JS code, not the cached code.

Comments

0

The Question mark (?) is just to handle the caching. It refreshes the file every time on the browser. We use the same technique to refresh the dynamically generated images also.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.