Reload JavaScript files without refreshing HTML

I’ve a HTML which loads several Javascript files:

<script src=""></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?

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);

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.

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);

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

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


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

Read More:   Large AngularJS application design

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts