...

View Full Version : JS script with parameters



sAdFella
03-11-2012, 12:22 PM
Hello,

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?

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

Regards,
sAdFella

devnull69
03-11-2012, 12: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, 12: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>');
</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;
d.getElementsByTagName('head')[0].appendChild(d.createElement('script')).src=u;
}

addScript("code.js?"+Math.random());



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum