View Full Version : JS script with parameters

03-11-2012, 01:22 PM

I have a question.

If I include js like this:
<script type="text/javascript" src="code.js?ver=1"></script>

If this include makes browser to always download js script? or does he use script from catch?

I need this for changing ver number for browser to know when to redownload script.

maybe is other way to redownload js script just when js script content is changed?

Sorry for my bad english. (I hope you understand what I wanted to ask) :)


03-11-2012, 01:36 PM
If the "src" attribute of the script tag doesn't change the browser will (depending on the cache settings) preferrably get the script from the cache. To circumvent the cache you could append an additional random parameter.

rnd me
03-12-2012, 01:41 AM
to download each time, you need a different url, which means you need a different script tag.

the safest way i can show you to do that without seeing the rest of the page is like this:

<script type="text/javascript">
document.write('<s'+'cript src="code.js?ver="'+(+new Date)+'></'+' script>');

document.write() does slow down page rendering some. if you don't have any dependencies that need the dynamic file in later <script> tags , you can use the more modern script adder:

function addScript(u){
var d=document;