简体   繁体   中英

How can I reload modified .js files from the browser console?

Can I use RequireJS 2 to reload a recently updated.js file? Here is my use case:

  1. I have a Backbone.js object called Foo with the function Bar that simply does alert("abc") ;
  2. In a webpage, I somehow call Foo.Bar() and get an alert thing with "abc"
  3. In my text editor, I change that function's definition to do instead alert("def")
  4. In the same webpage, I open the console and run some update function
  5. When I call again Foo.Bar() , I get the alert with "def"

Thank you!

You can acess the chome console by pressing: Ctrl+Shift+J And going to the "Console" Tab.
There Type the following:

var script = document.createElement("script");
script.src = "Path to Js File";
document.body.appendChild(script); 

It should update the JavaScript file without reloading the whole page.

  • Path to file Could be:
    1) http/s://yourdomain.com/path/jsfile.js
    2) file://path/jsfile.js - (Local file system)

Note:
The method used above does not require the RequireJS Library.

As you need to (load AND refresh) the js file, I suggest to define a function in your base javascript file like

function reloadJS(file) {
  var script = document.createElement("script");
  script.src = "/" + file + ".js";
  myWindow = window.open(script.src + "?v=" + Math.random());
  document.body.appendChild(script);
  myWindow.close();
 }

and call it from console like reloadJS("myjsfilename")

this will reload the js file and force it to update the file for browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM