3

I am attempting to edit some javascript code that is in the html of the page (not an imported js file). I am able to set break points and step through them, but I can not edit the javascript during the execution or before/after execution. I prettified ({}) and un-prettified the files. The code piece is not minified in this section.

  1. Can I do this?
  2. Does it matter that the code is inside an attached event. Ie a click etc.
  3. I am useing jquery obviously.

I could have sworn this used to be a common feature. But it has been over a year since I have done a lot of javascript.

Pavan Teja
  • 3,192
  • 1
  • 15
  • 22
Dan
  • 741
  • 1
  • 10
  • 22
  • Is the `javascript` in existing `html` page , or only run at `console` ? – guest271314 Feb 27 '16 at 20:01
  • The javascript is in the html of the page. – Dan Feb 27 '16 at 20:02
  • See http://stackoverflow.com/questions/18077217/is-there-a-way-to-save-css-js-changes-of-remote-resource-between-page-reloads-or/ , – guest271314 Feb 27 '16 at 20:43
  • 1
    The problem with that is all of them are creating full "workspaces" and doing a lot more then what I want. I know that I used to be able to make small changes to my javascript and see it work from the dev tools. Is this just not possible any more? do I have to create a full workspace just to do simple changes? – Dan Feb 27 '16 at 21:01
  • 1
    It does definitely still work. Try moving your code into a separate JavaScript file. After prettifying you won't be able to edit "script.js:formatted", but you can edit the minified "script.js" file. – Matt Zeunert Feb 27 '16 at 23:08

1 Answers1

5

Using chromium / chrome there are several methods to modify the html of an existing document. At devtools

  • Select Elements tab, right click on the element to modify, select Edit as HTML , make modifications in frame containing element, then click outside of editor frame

  • Select Sources tab, select Snippets tab, right click and select New , write javascript, to execute in existing window press at right panel to run javascript in Snippets middle panel in existing window. For example if $("body").on("click", function() {alert(123)}) is added as a Snippet clicking body element should call alert(123). The event should also be listed in Event Listeners at right panel of devtools when inspecting element. Removing the listener may be somewhat more challenging; even if you click remove when hovering over the listener at right panel, as the event is already attached to the element. The simplest method would be to add a namespace to the event $("body").on("click.abcnamespace", handler), then call $("body").off("click.abcnamespace")

  • Modifying text existing handlers will not automatically affect , or cancel the event handler previously attached to the element. The simplest approach would be to copy and save existing javascript containing event handler, select Elements tab , right click on element that has event listener, select Event Listeners at right panel, when hovering over the window, document or HTMLElement having event attached a button should be displayed that says Remove. Click that button to remove the event listener. You should then be able to modify the saved event listener and add it back to the existing document with modifications being applied

guest271314
  • 1
  • 15
  • 104
  • 177