Is there anything equivalent to php’s Domdocument class for javascript or Angularjs?

Asked

Viewed 56 times

1

Situation: Given the url, you would need all the classes used inside the page.

There is a class method DomDocument that I would need to use (->loadHTMLFile($url)).

There is something equivalent to this class for javascript or angularjs?

  • I needed to carry a url and then do a search for tags (I would like to get only the classes)

    $dom = new DOMDocument;
    $dom->loadHTMLFile('https://www.youtube.com/'); // a url vai aqui
    $elements = $doc->getElementsByTagName('input');
    
    foreach($elements as $el){
        //retorna todas as classes referente aos inputs
        echo $el->getAttribute('class')."\n";
    }
    

In javascript it is possible to only search for the current page (where the script is inserted), for example:

  • I wanted to make a die url and give the search for classes

    var $elements = document.getElementsByTagName('input');
    var show = "";
    for(var i = 0; i < $elements.length; i++){
        var tagClass = $elements[i].getAttribute('class');
        if( tagClass !== null){
            show += tagClass ;
        }
    }
    alert(show);
    
  • 1

    Do you want to use Javascript to access an external page? Have to see the issue of CORS, in some sites you can not make requests via Javascript. Maybe PHP is the best option in this case. Now, if you’re talking about looking up an HTML that you’re manipulating on your page yourself, you can do it.

  • What information you want to get from Youtube, there are alternatives. Or you can make a "web proxy" with php (with preferred cache) even and then pass it on with Ajax to Javascript.

  • Xpath would not serve in this context ?

  • @Guilhermenascimento I can be wrong, but it seems that he wants to take by the class and the domDocument has not, then the xpath resolves, I believe that is why he wants to use js... I may be wrong...

  • @Magichat the problem really is the "download", catch the tags would be the step "2".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.