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 forclasses
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);
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.
– Wallace Maxters
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.
– Guilherme Nascimento
Xpath would not serve in this context ?
– MagicHat
@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
@Magichat the problem really is the "download", catch the tags would be the step "2".
– Guilherme Nascimento