0

I want to reload and re-execute some javascript resources after some event happens. I looked up how to do this and found this SO question to be helpful. It says to remove the javascript resource and then create a new script tag referencing the same resource and append it to the head or body.

I tried it and it works.

I am using this code:

var reloadScripts = function(){
    var len = LOCALBROWSEVARIABLES.filesToReload.length;
    for (var i = 0; i < len; i++){
        reload_js(LOCALBROWSEVARIABLES.filesToReload[i]);
    }
}

function reload_js(src) {
    $('script[src="' + src + '"]').remove();
    $('<script>').attr('src', src).appendTo('head');
}

This code works for me. I can tell it works because I can see the outcome of the javascript functions that are being re run. But one of my files is not working as expected and I want to debug it. The problem is that when I put break points anywhere in the files that are being reloaded, they work when I first open the page, but not when they are reloaded. So I am not able to debug these reloaded scripts. Does anyone know why debugging/break points is not working when the scripts are being reloaded?

Community
  • 1
  • 1
user3494047
  • 1,643
  • 4
  • 31
  • 61
  • 1
    that really doesn't reload other than rexecute it. – Daniel A. White Dec 05 '16 at 16:45
  • 1
    well it is not exactly the same file that you opened in the console when you reload it. You are appending a new file to the page that happens to have the same name. – epascarello Dec 05 '16 at 16:46
  • 3
    Rather than adding the breakpoints using your browser console, you could try adding the `debugger;` command to the code itself. See eg http://stackoverflow.com/questions/10050465/set-a-javascript-breakpoint-in-code-in-chrome – Chris Lear Dec 05 '16 at 16:49
  • @DanielA.White if it re-executes, then shouldn't break points work? – user3494047 Dec 05 '16 at 16:51
  • @ChrisLear that works! Thanks a lot. – user3494047 Dec 05 '16 at 16:52
  • @epascarello I see. That would explain why it is not working – user3494047 Dec 05 '16 at 16:53
  • *As a side note:* `I want to reload and re-execute some javascript resources after some event happens` To me, it looks like a [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You usually don't have to reload any resource on page – A. Wolff Dec 05 '16 at 16:53
  • @A.Wolff I don't think its an XY Problem. I have problem X which i am mentioning for context purposes. I have a solution Y (which does work). Now, independent from problem X I have problem A. Please help me solve problem A. You are suggesting that solution Y doesn't solve problem X. I would love to talk to you about it, but I don't think its relevant to this SO question. Would you like to chat about it? – user3494047 Dec 05 '16 at 17:01
  • @user3494047 But why would you 'want' to reload some javascript scripts? That's the main question in fact. Whatever the reason you would need it, there is probably better way to handle that. Because anyway, reloading a script doesn't remove any previously included ones from browser memory. In your code, `$('script[src="' + src + '"]').remove();` is quite useless indeed – A. Wolff Dec 05 '16 at 17:07

0 Answers0