0

I'm trying to search in an array of urls if there is a class name in any of the pages and get a new array with the urls that have thas class name.

This is what I have:

var urls = ["https://www.url.com/withclass","https://www.url.com/withoutclass"];
var classToFind=document.getElementsByClassName('empty');
var results = [];
 
function findClassURL(url, classempty){
    jQuery.ajax({
        url:      url,
        dataType: 'text',
        type:     'GET',
        complete:  function(classname){
            if(typeof classempty === 'function')
                classempty.apply(this, [classname.all]);
        }
    });
}

for(var i = 0; i < urlsToSearch.length; i++) {
    findClassURL(results.push(urlsToSearch[i]), function() {
        if(all){
            console.log(results + '   ok');
        }
        else {
            console.log(urlsToSearch + '   has products');
        }
    });
}

Right now this gets me an 404 error.

Please help me to know if there is any other way of doing this

Thanks

E.quest
  • 1
  • 2
  • 1
    You misunderstand what “class name” actually means. – Dai Dec 20 '21 at 19:07
  • These are just strings and substrings from a programmatic point of view. You're distracting from the problem by calling them URLs and classes. – isherwood Dec 20 '21 at 19:11
  • Does this answer your question? [Filter strings in Array based on content (filter search value)](https://stackoverflow.com/questions/35235794/filter-strings-in-array-based-on-content-filter-search-value) – isherwood Dec 20 '21 at 19:12

1 Answers1

0

Before your for loop, I'd define an empty array to hold your results in (the urls that have the className in them. Like so:

let results = [];

Then, if a given url has the className, you add the url to the results array like so:

results.push(urlsToSearch[i]

Then at the end of your function, return the results like so:

return results;

mars
  • 147
  • 12