Skip to content Skip to sidebar Skip to footer

Find A List Of Directories On Server Side Virtual Directory And Print Out The List Using Javascript

I'm new at this web stuff so please bear with me. I'm attempting to read a list of directories from the server and print them out on the web page. I am using Tomcat and have direct

Solution 1:

If I recall correctly, on the directory list, Tomcat includes a slash after all folder names, but not file names. You should be able to check for that in your javascript to detect which one is a folder.

for (let i = 0; i < fileList.length; i++) {
    let file = fileList[i];
    if (file.endsWith("/")) {
       // Should be a directory
    }
}

Edited answer to work with html included:

fetch(YOUR_URL).then(resp=>resp.text()).then(html=> {
    // Use an iframe to allow for parsing of HTML easilylet iframe = document.createElement("iframe");
    // Must add iframe to body for it to be able to work
    iframe.style.display = 'none';
    document.body.append(iframe);

    // Set the content of the iframelet idoc = iframe.contentDocument || iframe.contentWindow.document;
    idoc.write(html);

    // Take the links inside the first table because there's probably other links on the pagelet links = idoc.getElementsByTagName('tbody')[0].getElementsByTagName('a');
    for (let i = 0; i < links.length; i++) {
        if (links[i].getAttribute("href").endsWith('/')) {
            console.log(links[i].getAttribute("href"));
        }
    }
    document.body.remove(iframe);
});

Post a Comment for "Find A List Of Directories On Server Side Virtual Directory And Print Out The List Using Javascript"