简体   繁体   中英

Testing all element nodes in the DOM for attribute existence

I'm trying to find if any of the tags inside a page contain a js event attribute, such as onload, onunload, onfocus etc, ... To do so I have an array of events to check for (I think I have them all):

var attr = [
"onLoad",
"onunload",
"onclick",
"ondblclick",
"onmousedown",
"onmouseup",
"onmouseover",
"onmousemove",
"onmouseout",
"onfocus",
"onblur",
"onkeypress",
"onkeydown",
"onkeyup",
"onsubmit",
"onreset",
"onselect",
"onchange"]; 

and then I get all the elementsByTagName and want to check for their attribute:

var getAttrs = function() {
var i = 0,
    j,
    all = document.getElementsByTagName('*'),
    max = all.length,
    eventsLen = attr.length;


for (; i < max; i++) {

    console.log('iterating through objects');

    for (j = 0; j < eventsLen; j++) {

       if (all[i].hasAttribute(attr[j])) {
          console.log('found an instance of an intrinsec event', attr[j]);
       }

    }

}

};

This isn't working and I'm not getting any of the attributes I wanted to find.

Additionally, I'd like to find all attributes within all tags that use javascript: (such as in <a href="javascript:void(0)">) Any idea how I could add that to the function's logic?

Thanks!

If you want to get all elements which have either attribute, the following code can be used:

var attr = ["onLoad", "onunload", "onclick", "ondblclick", "onmousedown",
            "onmouseup", "onmouseover", "onmousemove", "onmouseout", "onfocus",
            "onblur", "onkeypress", "onkeydown", "onkeyup", "onsubmit", "onreset",
            "onselect", "onchange", "href^='javascript:'"];
var selector = "[" + attr.join("],[") + "]";
//selector looks like "[onLoad][onunload]...[href^='javascript:']"
var eitherAttribute = document.querySelectorAll(selector);
for(var i=0, len=eitherAttribute.length; i<len; i++){
    console.log(eitherAttribute[i]); //Logs the element
}

If you want to select all elements which match multiple attributes, use:

//Example: onclick, AND href starting with javascript:
document.querySelectorAll("[onclick][href^='javascript:']");

For more information, see MDN: document.querySelectorAll .
If you need more flexible selectors, have a look at: MDN: Selectors

Change:

if (all[i].hasAttribute(attr[j])) {

To:

if (all[i][attr[j]]) {

As it can also be a property and as opposed to an attribute.

This is how I would do it:

var checkForEventAttrs = (function () {
    var eventAttrs = [ /* a list of attribute name strings */ ];

    function walkTheDOM( node, func ) {
          func( node );
          node = node.firstElementChild;
          while ( node ) {
              walkTheDOM( node, func );
              node = node.nextElementSibling;
          }
     }

    return function () {
        walkTheDOM( document.body, function ( node ) {
            eventAttrs.forEach( function ( eventAttr ) {
                if ( node.hasAttribute( eventAttr ) ) {
                    console.log( 'The attribute ' + eventAttr + ' found.' );
                }
            });
        });
    };
})();

and then just call that function to log those attribute into the console:

checkForEventAttrs();

Live demo: http://jsfiddle.net/F5AEy/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM