Rad'Val
Rad'Val

Reputation: 9231

How can you see which Javascript script generated a certain html line?

I have some crazy app done almost 100% by manipulating the DOM and I find myself in the unfortunate position of changing something in it. As there are probably around 100 different scripts that do God knows what, I have no clue in which file should I look to make my changes. So, I want to ask, is there a way (using Firebug maybe or something similar) to know where a specific piece of html was generated? I'm a C developer, so I'm not very good at this, it drives me crazy.

Upvotes: 3

Views: 4951

Answers (4)

jakub.g
jakub.g

Reputation: 41238

Are all the elements added at the page load, or partially in the response to the user input? (clicking etc.)

  • for stuff added with the response to your actions, you can use Firebug's "Break On Next" button in the "Script" tab. To active BON you have to click it, or, in just-shipped Firebug 1.10.0a8, use keyboard shortcut ALT-CTRL-B (useful when you have event listeners bound to mouse movements). Then, when any piece of JS is going to be executed in reaction to your click etc., you will hit a breakpoint.
  • for stuff added at page load time, you may use the trick of extending the native functions (this might sound crazy - yeah it is, don't do it in production!) like appendChild, insertBefore, replaceChild. Just insert the appropriate code at the very top of your main HTML file, so all the code below will "see" the change. Unfortunately, this does not work in Firefox due to a bug. But works in Opera and I guess in Chrome as well. When you extend the native function, you can inject any code before really adding the node to the page. For instance, call console.log or create a breakpoint, to inspect the current page state. You can try playing with breakpoints to see the available variables properties inside those function to adjust what you push to console.log.

For this code:

<!doctype html>
<html>
   <head>
      <script type="text/javascript">

      // this should work in Firefox but it does not -- https://bugzilla.mozilla.org/show_bug.cgi?id=618379
      // works at least in Opera, probably Chrome too
      Node.prototype._appendChild = Node.prototype.appendChild;
      Node.prototype.appendChild = function(child) {
         console.log("appending " + child + " to " + this);
         return this._appendChild(child); // call the original function with the original parameters
      }

      // this works in Firefox
      document._createElement = document.createElement;
      document.createElement = function(tagName){
         console.log("creating " + tagName);
         return this._createElement(tagName);
      }

      </script>
   </head>

   <body>
      <script type="text/javascript">
         var p = document.createElement("p");
         p.appendChild( document.createTextNode("abc"));
         document.body.appendChild(p);
      </script>
   </body>
</html>

Opera outputs:

creating p                                                           appendChild.html:14
appending [object Text] to [object HTMLParagraphElement]             appendChild.html:7
appending [object HTMLParagraphElement] to [object HTMLBodyElement]  appendChild.html:7

To overcome the weakness of Firefox (that you can't override appendChild), you may use the trick: place the code below instead in the top of your HTML

<script>
      Node.prototype._appendChild = function(child) {
         console.log("appending " + child + " to " + this);
         return this.appendChild(child)
      };
</script>

and then, use Fiddler proxy by creating auto-responders (WMV tutorial, 9.9 MB) where you manually replace all calls to .appendChild with ._appendChild (you can use Notepad++ for "find replace in all opened files"). Creating auto-responders and hand-tampering requests can be mundane, but it's extremely powerful. To quickly create auto-responder rule, load the page when Fiddler is active, then drag'n'drop files as in the picture below. For each file, right click and choose "Generate File" from menu (this will put a file on the desktop) or create a file by yourself in different location. (it's good to open Fiddler-generated files and remove response headers from them; BTW "Generate file" puts real contents only if the response header was 200, so make sure to load the page with CTRL-F5 to skip the cache).

Fiddler

Upvotes: 4

JKing
JKing

Reputation: 847

Assuming you've got access to the raw (hopefully un-minified/obfuscated) JS files, maybe just search them for text strings related to DOM manipulation and/or attributes of the node you're trying to find the creation of? I'd try things like "appendChild" "createElement" and the node's ID/class names.

You could also set break points all over the script files, and step through them as the page loads to help you narrow down where to look. Might help to start by just "pausing" the JS execution and stepping through from the very beginning.

If you can share the code (a link to the live site would do fine) I'd be happy to take a look.

Upvotes: 1

Brian Scott
Brian Scott

Reputation: 9361

If you are using the jQuery framework in your javascript to make the DOM changes then you may find the fireQuery plugin for FireBug in the firefox browser may get you the information you need.

Example: enter image description here

It adds additional information to the standard HTML view by superimposing additional jquery element information to the display to provide a deeper insight into how your javascript is amending the page content.

I hope that helps you out.

Upvotes: 0

ianneub
ianneub

Reputation: 367

In Chrome you can inspect an element and right click on it. This menu gives you some options to break when something below the element is changed or when it's own attributes change. Maybe one of those breakpoints will find what you are looking for?

Upvotes: 2

Related Questions