最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - What does “?”and a number behind after javascript is for? - Stack Overflow

programmeradmin1浏览0评论

I saw in some HP code that there is a question mark and a number after javascript file name and it seems like a date or something. What does “?” and a number behind after javascript is for? Example:

<script type="text/javascript" src="/folder/js/folderPageNextSubmit.js?20140801"></script>

I saw in some HP code that there is a question mark and a number after javascript file name and it seems like a date or something. What does “?” and a number behind after javascript is for? Example:

<script type="text/javascript" src="/folder/js/folderPageNextSubmit.js?20140801"></script>
Share Improve this question edited Jul 23, 2015 at 10:04 Johan 76.6k27 gold badges199 silver badges337 bronze badges asked Jun 21, 2015 at 5:24 CDMHCDMH 615 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 8

it is for cache breaking, for example:

 file.js?1234

 file.js?12345

the browser treats them as two different files, so even if the first one was cached so the second one will be fetched anyway.

you change the number after you do a change in the file code and deploy it, so for the users to see your new changes and load the new file, you change the number.

The "?" in JavaScript or CSS files is a way to add versioning to the file.

Is useful to avoid browser caching when you are working in dev environments because when the versioning number changes, the file URL also changes. So seems to be a different file for the browser.

Is a common practice use a timestamp as versioning number to get something unique but you can use any numbers or chars.

People typically use that to force users to update the browser cache. If I have a file on my page init.js and you visit my site, and then I update it, and you visit again, your browser will probably not download my updated file, but just use what's in it's cache.

Adding a random string or date will force the browser to update it's cache because it thinks the file is different.

The downside is I believe it will never cache it at all. Some browsers will download the file everytime if you use a URL paremeter vs doing something like init.1234.js so using the URL param method is probably not the best idea.

EDIT: Additionally it's better to set the expires header on your server if you need to update some crucial files, but I won't say I've never used this method, just not the best idea. I believe adding the date in the file itself is better.

In the web request? That anti-pattern is used so browsers are forced to never cache a file (or rather, always cache a different copy) because it always has a different URL.

Better programmers simply use the server/response cache profile setting and actually allow caching if files don't change.

It is a common strategy to force reloading a file instead of using the copy in the cache. Appending a timestamp this way forces the reload.

Generically, the ? in any URL signifies the end of the path and the beginning of query parameters.

So, in your script tag:

<script type="text/javascript" src="/folder/js/folderPageNextSubmit.js?20140801"></script>

The path to the file is "/folder/js/folderPageNextSubmit.js" and a query parameter ?20140801 has been added to the end of the URL.

It is totally up to the server whether it does anything with this specific query parameter or not. For example, the 20140801 could be a date code for 1/08/2014 that means the server will serve a specific version of that file that matches that date code.

Or, it could be that the server doesn't do anything with the query parameter and it is purely used to break browser or proxy caching of this JS file in prior URLs that used a different date code so that a newer version will be served rather than an older cached version.

Without knowing the specific use and how the server is implemented, one can't really tell which case it is.

发布评论

评论列表(0)

  1. 暂无评论