0

On my website, after loading a page, CSS height is adding to the DOM and I am unable to find from where it was modified. Is there any way to debug it?

Below is the demo example.

<!DOCTYPE html>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        <style>
            .div { border: 1px solid; }
        </style>
    </head>
    <body>
        <div class="div"> <b>hello</b> </div>
        <script>
            $(document).ready(function(){
                $('.div').css("height", "200px");
                $('body > div').css("height", "200px");
            });
        </script>
    </body>
</html>

DOM can modify from any file and in any way. How can I debug it on a large website?

Thank you.

Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
26vivek
  • 191
  • 1
  • 2
  • 9
  • @AlirezaAhmadi I think you missed these bits of the question: *below is the demo **example**.* and *DOM can modify from any file and in any way* – freedomn-m Jul 22 '21 at 10:46
  • Does this answer your question? [Detecting class change without setInterval](https://stackoverflow.com/questions/42121565/detecting-class-change-without-setinterval) – freedomn-m Jul 22 '21 at 10:47
  • 1
    Use [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) – freedomn-m Jul 22 '21 at 10:48
  • If you wanna try a more modern aproach try https://alpinejs.dev/. There is a big hipe about it recently. – Gkiokan Jul 22 '21 at 11:19

2 Answers2

0

You can listen to any resize on your element by ResizeObserver

        $(document).ready(function () {
            $('.div').css("height", "200px");
            $('body > div').css("height", "200px");
        });

        
        new ResizeObserver((e) => {
            console.log("cahnged");

        }).observe($('.div')[0])
.div {
            border: 1px solid;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
        
<div class="div"> <b>hello</b> </div>
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
0

You can observe all changes in the DIV element by using MutationObserver.

let observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
     console.log(mutation.attributeName);
  });
});

observer.observe($('#test')[0], {childList:true, attributes:true}); 

//test  
$('#test').height("50px")      
setTimeout(function(){$('#test').height("100px")},3000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="test">

https://developer.mozilla.org/en-US/docs/Web/API/MutationRecord

Kemal Kaplan
  • 932
  • 8
  • 21