24

I've a HTML which loads several Javascript files:

<script src="assets/js/a.js"></script>
<script src="assets/js/b.js"></script>
<script src="assets/js/jquery.min.js"></script>

As I debug/test my Javascripts in my browser's console, is it possible to reload these Javascript files without the need to reload the entire HTML page?

4
  • 1
    If you're debugging, you want to debug what the user experiences. If you just reload a script, you're starting from a different context then a user would. So reloading the entire HTML while debugging isn't such a bad thing. Commented Jun 3, 2012 at 6:12
  • 1
    good question. but it has already been asked and answered. Is there a way to refresh just the javascript include while doing development? Commented Jun 3, 2012 at 6:17
  • thanks for all the answers and comments. Commented Jun 6, 2012 at 15:28
  • I think the problem during debugging is even worse: reloading the HTML using Ctrl+F5 fails to reload any of the asset files, such as .JS files, which may have changed. Commented Sep 11, 2020 at 15:32

4 Answers 4

19

You could remove and then re-add them:

$('script').each(function() {
    if ($(this).attr('src') !== 'assets/js/jquery.min.js') {
        var old_src = $(this).attr('src');
        $(this).attr('src', '');
        setTimeout(function(){ $(this).attr('src', old_src + '?'+new Date()); }, 250);
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

@Truth - he has a jquery script element in the question code!
+1 to negate that -1. @Truth: 3 doesn't means all :) atleast at StackOverflow :) but PitaJ your code is buggy. 1. setTimeout wont does nothing in this code. 2. more importantly, replacing script wont work here. you can only add a new script Replacing script src using jquery not executed
I think you mean Date.now() instead of new Date()
2

Nope (at least not without a hack), in fact - be very careful reloading so much. It's quite possible that your javascripts become cached, which leads to many-a-headache trying to debug, since your updates are not applying.

https://superuser.com/questions/36106/force-refreshing-only-javascript-files-in-firefox-and-chrome

1 Comment

You can always reload with Ctrl+F5, which will refresh the cache
1

Based on PitaJ's solution.

Reload all javascript in a function. You can call this from anywhere you want.

 $('script').each(function () {
        if ($(this).attr('src') != undefined && $(this).attr('src').lastIndexOf('jquery') == -1) {
            var old_src = $(this).attr('src');
            var that = $(this);
            $(this).attr('src', '');

            setTimeout(function () {
                that.attr('src', old_src + '?' + new Date().getTime());
            }, 250);
        }
    });

Comments

0

Tested with jQuery 2.0.3 and chrome 43

 function reloadScript(id) {

    var $el =  $('#' + id);

    $('#' + id).replaceWith('<script id="' + id + '" src="' + $el.prop('src') + '"><\/script>');
}

Chrome is complaining, but works:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check http://xhr.spec.whatwg.org/.

Your script tag has to have id. Call in console:

reloadScript('yourid');

Does not remove event listeners, so for for example button click gets executed twice if once reloaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.