Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOM attribute change debugging

Tags:

Somehow somewhere in my code one of the elements on the page gets a style attribute which I don't expect it to get. Namely, it gets style="position:fixed". I can see this happening in HTML tab in Firebug, but can't find it in the code. The application is fairly big and I can't simply look through all of the code to find the place, besides, several third-party libraries are being used (jQuery is one of them).

So, my question is, is it possible to somehow catch this style being changed and get the trace?

like image 926
Maxim Sloyko Avatar asked Aug 10 '09 08:08

Maxim Sloyko


People also ask

How do you debug a DOM?

Debugging With Your BrowserRight clicking on an element in Chrome. You will see an option labeled 'Inspect'. An easy-to-read visual representation of your DOM will open, which can be modified and analyzed to track down any code that may be of issue.

How do you check attributes in DOM?

HTML DOM Element hasAttribute() The hasAttribute() method returns true if the attribute exists, otherwise false .

What is a DOM attribute?

The attributes property in HTML DOM returns the group of node attributes specified by NamedNodeMap objects. The NamedNodeMap object represents the collection of attribute objects and can be accessed by index number. The index number starts at 0.

How do I add attributes to a DOM element?

We can add an attribute to a DOM element using two methods. First, is the setAttribute(name, value) and second is the setAttributeNode(attribute_node) .


2 Answers

In Google Chrome, right click on an element in the page and select "Inspect Element." The Developer Tools window or pane will open with that element selected in the source view. You can then right click the selected tag and choose "Break on Attributes Modifications."

like image 186
rxgx Avatar answered Sep 30 '22 20:09

rxgx


Well, after a couple of hours of googling and experimenting, it seems that the best one can do it setup a MutationEvent handler (Firefox supports them) like this:

var node_modified = function(evt) {     if(evt.attrName == 'style') {         alert('Style is changing from ' + evt.prevValue + ' to ' + evt.newValue);     } } var test_close = document.getElementById('test_close'); test_close.addEventListener('DOMAttrModified', node_modified, false); 

An then set up some kind of logging throughout your code and see when this event gets triggered. Unfortunately, you can't just set up a breakpoint in the mutation event handler and see the stack trace, because event handler's stack trace has no information about the place in the code, where the event got triggered. Kind of logical, but I think that with some hacking this feature can be implemented in Firebug.

Thank you for your time!

like image 25
Maxim Sloyko Avatar answered Sep 30 '22 19:09

Maxim Sloyko