Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS script with parameters

    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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    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.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    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:

    Code:
    <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:



    Code:
    function addScript(u){ 
     var d=document;
     d.getElementsByTagName('head')[0].appendChild(d.createElement('script')).src=u;
    }
    
    addScript("code.js?"+Math.random());
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •