I am wondering how I can trigger a function using Jquery as soon as my $('html') resizes.
I tried:
$('html').on('resize', executeThisFunction);
But it looks like the event ist only triggering on the window element.
I am wondering how I can trigger a function using Jquery as soon as my $('html') resizes.
I tried:
$('html').on('resize', executeThisFunction);
But it looks like the event ist only triggering on the window element.
html doesn't fire a resize event, but you could look into monitoring DOM properties, or a plugin as described in this thread
window does fire have a resize event which is why it's working.
Technically speaking it is the window which resizes, not html.
Hence you need to trigger resize on window object.
You can try this way
$(document).ready(function () {
$(window).resize(function(){
//your code to be triggered on resize goes here
}
});
//triggering resize whenever your window is resized
$(window).trigger('resize');
}
Update
As asked in comment by OP user, if one needs to call a function when the content of div or html changes than it can be achieved in following way
$('html').bind("DOMSubtreeModified",function(){
//your code goes here
});
This method has a drawback that it is fired every time if there is a slight change in DOM tree. To overcome this you can use alternative method as follows:
$('#anydiv').bind('DOMNodeInserted DOMNodeRemoved', function() {
//your code goes here
});
Note: Opera and IE does not support DOMSubtreeModified. Also this event is depreciated. It is recommended to use MutationObserver instead.
try this
$(window).on('resize', executeThisFunction);
var h = $('html').heigth();
$( window ).resize(function() {
if (h != $(this).height(){
// do your code
}
});