Thursday, March 30, 2023

Coding Challenge #37 Find element with href in parents

// this traces through a given element's parents, trying to find any elements with an href
// wanted to find all inherited ways the click would send the user to another page

function traceAncestry(ele){
    var levelsAbove = 0;
    do{
        levelsAbove++;
        ele = ele.parentElement;
        if(ele.href != undefined && ele.href != ""){
            console.log("====== LV " + levelsAbove + ", vvv link to: " + ele.href);
            console.log(ele);
        }
    }while(ele != document.body && ele != null && levelsAbove < 1000);
    
    if(levelsAbove == 1000) console.log("Quit due to timeout. Probably infinite loop.");
    console.log("Traced through " + (levelsAbove-1) + " levels, checking for HREF attribute.");
}

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...