Force page load to always reload external JS library

Discussion in 'Javascript' started by Mark Anderson, Mar 1, 2005.

  1. Sorry if this is borderline JS vs. HTML. I ask here as the
    success/failure if the resulting JS is key.

    I've a small external .JS library that allows a non code-savvy user to
    occasionally update a list of values that are read as a JS array and
    used to populate the <option>s of a <select> in the calling page's form.

    Anyway, is there a way by which I can ensure, regardless of the user's
    browser settings, that the JS library is not used from cache but re-read
    each time, to ensure the up to date list is used?

    If not, I might as well put the JS array code back in the calling page,
    especially as the split is for ease of use by the page's owner, albeit
    with increased change of editing errors. A database solution, etc., is
    inappropriate here.

    TIA,

    Mark
    Mark Anderson, Mar 1, 2005
    #1
    1. Advertising

  2. Mark Anderson

    Randy Webb Guest

    Mark Anderson wrote:

    > Sorry if this is borderline JS vs. HTML. I ask here as the
    > success/failure if the resulting JS is key.
    >
    > I've a small external .JS library that allows a non code-savvy user to
    > occasionally update a list of values that are read as a JS array and
    > used to populate the <option>s of a <select> in the calling page's form.
    >
    > Anyway, is there a way by which I can ensure, regardless of the user's
    > browser settings, that the JS library is not used from cache but re-read
    > each time, to ensure the up to date list is used?
    >
    > If not, I might as well put the JS array code back in the calling page,
    > especially as the split is for ease of use by the page's owner, albeit
    > with increased change of editing errors. A database solution, etc., is
    > inappropriate here.


    You dynamically create the script tag with a unique paramater to it.

    <script type="text/javascript">
    document.write('<script type="text/javascript"
    src="myFile.js?' + new Date().getTime() + '">
    <\/script>');
    </script>

    It is split across lines for readability only.
    The idea is that the parameter in the file name should always be unique
    since you are appending the current time to the filename. Since the
    filename is unique, it can not be retrieved from the cache.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, Mar 1, 2005
    #2
    1. Advertising

  3. "Randy Webb" <> wrote in message
    news:...
    > You dynamically create the script tag with a unique paramater to it.
    >
    > <script type="text/javascript">
    > document.write('<script type="text/javascript"
    > src="myFile.js?' + new Date().getTime() + '">
    > <\/script>');
    > </script>
    >
    > It is split across lines for readability only.
    > The idea is that the parameter in the file name should always be
    > unique since you are appending the current time to the filename. Since
    > the filename is unique, it can not be retrieved from the cache.


    I get the idea but it doesn't work as the browser can't find the
    referenced JS file as its filename is missing the date info added by the
    dynamic reference. I've tested the solution - IE & Firefox both fail.

    I used (lines may wrap):
    ~~~HTM~~~
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled</title>
    <script type="text/javascript">
    document.write('<script type="text/javascript" src="myFile.js?' + new
    Date().getTime() + '"><\/script>');
    </script>
    </head>
    <body>
    Text Line 1 in HTM file source<br><br>
    <script type="text/javascript">
    writeMe();
    </script>
    </body>
    </html>
    ~~~End HTM~~~
    ~~~JS~~~
    function writeMe(){
    document.write('Text Line 1 from JS file source<br><br>Text Line 2
    from JS file source.')
    }
    ~~~End JS~~~

    Thanks,

    Mark
    Mark Anderson, Mar 2, 2005
    #3
  4. Mark Anderson

    Lee Guest

    Mark Anderson said:
    >
    >"Randy Webb" <> wrote in message
    >news:...
    >> You dynamically create the script tag with a unique paramater to it.
    >>
    >> <script type="text/javascript">
    >> document.write('<script type="text/javascript"
    >> src="myFile.js?' + new Date().getTime() + '">
    >> <\/script>');
    >> </script>
    >>
    >> It is split across lines for readability only.
    >> The idea is that the parameter in the file name should always be
    >> unique since you are appending the current time to the filename. Since
    >> the filename is unique, it can not be retrieved from the cache.

    >
    >I get the idea but it doesn't work as the browser can't find the
    >referenced JS file as its filename is missing the date info added by the
    >dynamic reference. I've tested the solution - IE & Firefox both fail.
    >
    >I used (lines may wrap):
    >~~~HTM~~~
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    ><html>
    ><head>
    ><title>Untitled</title>
    ><script type="text/javascript">
    >document.write('<script type="text/javascript" src="myFile.js?' + new
    >Date().getTime() + '"><\/script>');


    The server will ignore the "?" and anything following it when it looks
    for the .js file. If it's failing to find it, it's because you don't
    have a file named "myFile.js" in the base directory.
    Lee, Mar 2, 2005
    #4
  5. "Lee" <> wrote in message
    news:...
    > Mark Anderson said:
    > The server will ignore the "?" and anything following it when it looks
    > for the .js file. If it's failing to find it, it's because you don't
    > have a file named "myFile.js" in the base directory.
    >


    Yes, apologies - the problem was a naming error. All working!

    Thanks everyone,

    Mark
    Mark Anderson, Mar 2, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sia Jai Sung

    Force reload of ASP page after processing

    Sia Jai Sung, Jan 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    8,166
  2. UJ
    Replies:
    6
    Views:
    75,946
    pcpgmr
    Mar 23, 2010
  3. tshad

    no-cache to force reload new page

    tshad, Jul 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    822
    tshad
    Jul 27, 2006
  4. Nige

    Force reload of changed page?

    Nige, Nov 14, 2003, in forum: Javascript
    Replies:
    3
    Views:
    129
    Thomas 'PointedEars' Lahn
    Nov 18, 2003
  5. pbd22
    Replies:
    4
    Views:
    218
    Kailash Nadh
    Nov 29, 2007
Loading...

Share This Page