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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    21
    Thanks
    3
    Thanked 1 Time in 1 Post

    Refresh an external javascript file from the server at intervals?

    Hi guys,

    I have a webpage in which certain content will be called from a javascript file.

    I will be linking to the file as follows:

    Code:
    <script type="text/javascript" src="js.js"></script>
    The js.js file will be updated by an admin in my office.

    Is it possible to have the js.js file called from the server periodically - say every 1800 seconds (to update the webpage without interrupting the user)?

    Also can there be a link to manually call the js.js file as well, in case a user doesn't want to wait 1800 seconds for the next update?

    I'm aware of the meta refresh tag but this would interrupt the user and erase text from the form fields that they need to use throughout the day.

    Your help is greatly appreciated.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    the exact approach depends on the code, but you generally would want to use an "ajax refresh" of some type. searching the forums will bring up numerous examples. what does js.js do?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,436
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Yeah, Dan, but what he wants to do is *REPLACE* js.js

    That can be done, and probably should be done using AJAX, but it's not a simple proposition.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    That can be done, and probably should be done using AJAX, but it's not a simple proposition.
    It wouldn't be simple to do with AJAX and so shouldn't be done that way (unless you are using AJAX as a broad term to mean calling the server). It is really easy to do this with just a few DOM commands.

    First you need a slight modification to the script tag.

    Code:
    <script type="text/javascript" src="js.js" id="js"></script>
    You then use the following script to replace the script tag every 1800 seconds appending a different querystring to the src each time to prevent the currently cached copy being reused.

    Code:
    setInterval(
       function() {var d, s;
           d = document.getElementById('js'); 
           s = document.createElement('script');
           s.src = 'js.js?d='+ Date.now();
           d.parentNode.removeChild(d);
           s.id = 'js';
           document.getElementsByTagName('head')[0].appendChild(s);
           },
       1800000);
    Last edited by felgall; 09-24-2012 at 11:21 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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