Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Below is my code which tries to parse the HTML of the current page using JavaScript: 

var parser = new HTMLtoDOM("<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>", document);

My goal is to extract links from an HTML external page that I read just like a string.

When I run the above code, it changes the title of my page. Can anyone help me to do this?

1 Answer

0 votes
by (19.7k points)

You can create a dummy DOM element and add the string to it like below:

var el = document.createElement( 'html' );

el.innerHTML = "<html><head><title>titleTest</title></head><body><a href='test0'>test01</a><a href='test1'>test02</a><a href='test2'>test03</a></body></html>";

el.getElementsByTagName( 'a' ); // Live NodeList of your anchor elements

Interested in Java? Check out this Java tutorial by Intellipaat. 

Browse Categories

...