I am building a toolbar that is going to be included into a page. the div it is going to be included in will default to display:none. Is there a way i can p
Just to comment on the DOMAttrModified event listener browser support:
Cross-browser support
These events are not implemented consistently across different browsers, for example:
IE prior to version 9 didn't support the mutation events at all and does not implement some of them correctly in version 9 (for example, DOMNodeInserted)
WebKit doesn't support DOMAttrModified (see webkit bug 8191 and the workaround)
"mutation name events", i.e. DOMElementNameChanged and DOMAttributeNameChanged are not supported in Firefox (as of version 11), and probably in other browsers as well.
Source: https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Mutation_events
var targetNode = document.getElementById('elementId');
var observer = new MutationObserver(function(){
if(targetNode.style.display != 'none'){
// doSomething
}
});
observer.observe(targetNode, { attributes: true, childList: true });
I might be a little late, but you could just use the MutationObserver to observe any changes on the desired element. If any change occurs, you'll just have to check if the element is displayed.
As @figha says, if this is your own web page, you should just run whatever you need to run after you make the element visible.
However, for the purposes of answering the question (and anybody making Chrome or Firefox Extensions, where this is a common use case), Mutation Summary and Mutation Observer will allow DOM changes to trigger events.
For example, triggering an event for a elements with data-widget
attribute being added to the DOM. Borrowing this excellent example from David Walsh's blog:
var observer = new MutationObserver(function(mutations) {
// For the sake of...observation...let's output the mutation to console to see how this all works
mutations.forEach(function(mutation) {
console.log(mutation.type);
});
});
// Notify me of everything!
var observerConfig = {
attributes: true,
childList: true,
characterData: true
};
// Node, config
// In this case we'll listen to all changes to body and child nodes
var targetNode = document.body;
observer.observe(targetNode, observerConfig);
Responses include added, removed, valueChanged and more. valueChanged
includes all attributes, including display
etc.