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 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts

    Smile '$.getScript()' question!

    Hey CodingForums and coders!
    I'm coding plugin for a website and websites has good APIS to example check when someone joins to chat and to send automatically message: 'Welcome' etc.

    So I'm coding /update command to my 'chatbot' and was goin to use $.getScript to get newest, updated script from url and run it again / replacing old code.

    So everytime when I send to chat '/update' chatbot will run function update.
    What do I need to do to replace the old code with the new code?

    Here's something what I tried and I think it's working, or is it?
    Code:
    $.getScript(url,function(){
    	// Sends message 'chabot is updated' and get's version from variable chatBotV!
    });
    Also nice add would be to check if olds code version (variable chatBotV) is same as updated, it wont replace it.
    If you need more details comment below...
    ~Thanks, ECode

  • #2
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts
    I'm sure someone could help me out even a bit!
    *BUMP*!

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,142
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by ECoode View Post
    I'm sure someone could help me out even a bit!
    *BUMP*!
    We are not some sort of public utility standing around here 24/7 just waiting for your questions. Please have a look at forum Guideline #5

    5) Do not bump your thread repeatedly when you don't get a response. Sometimes when you post for help, you may not get a response in a timely matter, if at all. Forums aren't wishing wells, and some questions will fall through the cracks. That's a fact of life. It's ok to occasionally bump a thread, but only when done after an ample amount of time (ie: 2-3 days) have passed without a response, and never more than once. Your thread is no more important than another member's when it comes to the amount of attention it should receive.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Ok I'm trying to re-post this to other forums if one of them could answer

  • #5
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Couple days since no1 answered!
    So now I can *bump* *bump* it !

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you just want to force the script to download when they first access the page rather than using a cached copy then just add a querystring on the end of the src attribute of the script tag with a value that is always changing (the easiest way is to use server side code to insert the date/time).


    To replace a JavaScript with the latest version dynamically at regular intervals while they are still on the page you just need to run the appropriate DOM commands to replace the script tag in the page and add a different value in the querystring so that it forces it to redownload the script rather than usung the cached copy.

    for eexample the following will reload script.js from the server every five minutes (you just need to add id="js" to the script tag so it knows which tag to replace)

    Code:
    setInterval(
       function() {var d, s;
           d = document.getElementById('js'); 
           s = document.createElement('script');
           s.src = 'myscript.js?d='+ Date.now();
           s.id = 'js';
           d.parentNode.replaceChild(s,d);
           },
       300000);
    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.

  • Users who have thanked felgall for this post:

    ECoode (10-04-2012)

  • #7
    New Coder
    Join Date
    Aug 2012
    Location
    Finland
    Posts
    23
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by felgall View Post
    If you just want to force the script to download when they first access the page rather than using a cached copy then just add a querystring on the end of the src attribute of the script tag with a value that is always changing (the easiest way is to use server side code to insert the date/time).


    To replace a JavaScript with the latest version dynamically at regular intervals while they are still on the page you just need to run the appropriate DOM commands to replace the script tag in the page and add a different value in the querystring so that it forces it to redownload the script rather than usung the cached copy.

    for eexample the following will reload script.js from the server every five minutes (you just need to add id="js" to the script tag so it knows which tag to replace)

    Code:
    setInterval(
       function() {var d, s;
           d = document.getElementById('js'); 
           s = document.createElement('script');
           s.src = 'myscript.js?d='+ Date.now();
           s.id = 'js';
           d.parentNode.replaceChild(s,d);
           },
       300000);
    Thanks for this very useful information!
    I really appreciate that you answered, thanks again !


  •  

    Posting Permissions

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