0

I want to make changes through the sources tab in Inspect Elements and want to see a preview quickly without refreshing the browser.

Is there a way to do this?

For example, in JavaScript I have a variable:

var abc = 'xyz';

But through the Sources tab, I got to my script file and changed the code:

var abc = 'hello world';

Now if I try alert(abc) then, will it show latest changes? I don't want to refresh to see this change.

Is there any kind of possibility or plugin that can do this? Because sometimes this is very time saving. For example, each time I test I have to do a lot of steps to see some value or response. So for making a minor change I have to change it in the original file and refresh again to see it and this way I always have to go through the complete process that wastes a lot of time.

1 Answer 1

2

You can use Chrome's Developer Tools (press F12 or Ctrl+Shift+I) to debug and manipulate JavaScript code at runtime. You can then set breakpoints in your code, examine objects, play with values, call functions from the Console and more. All without having to actually change your physical source files or reloading the window.

Update following OP's comment below:

Here's a quick-and-dirty example of creating a new function on the fly and then calling it in the console. The first line adds the function (adding the function itself does not return anything meaningful so you get an "undefined" result). Next I call this new function which as expected pops up an alert window (not shown in the screenshot) and returns the value 17.

Enter image description here

You can do the same and return values calculated from your actual page in its current state. If you placed a breakpoint in your code you can use the console to examine objects and values or perform changes to them at runtime. BTW, this also applies to the DOM, so you can manipulate the HTML as well through the console.

4
  • Thanks for your message. I tried your method but that did not work. I added a function through sources tab and called that function from console but it said undefined Commented Sep 6, 2016 at 0:02
  • I updated my answer above with a sample screenshot and explanation.
    – Atzmon
    Commented Sep 6, 2016 at 7:21
  • I know that it is possible to update function through console. But I want to change from existing source in the source tab or some other way like that. Because sometimes I have very complex and long functions that normally cannot be pasted in console. So is there any better way for that ? Commented Sep 6, 2016 at 8:13
  • Yes. Assuming your javascript is in external files (not embedded script chunks in the HTML), put a breakpoint before the part you want to change, then when the debugger hits that breakpoint simply change the source within the Sources tab and continue debugging. Your new code will run. Be advised that this does not change your actual code files (your changes will be gone when you refresh or exit Chrome).
    – Atzmon
    Commented Sep 6, 2016 at 9:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .