JAVASCRIPT - HOW TO Extract All Links from URL/Web Address - Javascript Source Code

JAVASCRIPT - HOW TO Extact All the Links (or any other html element you wish) from a Web Page URL/Web Address - NOT from HTML Code but an Actual Web Address/URL

Home Short:

In Javascript you can scan a live web page and extract elements from it, just like you can in php. The main difference is with Javascript, you are mostly restricted to being able to scan web pages that reside on the same url as where the Javascript code resides due to known as Access Control Allow Origin which is a security feature that prevents a website from crawling and fetching content from a different website. Why html standards allow this to occur with PHP code and not Javascript I am unsure of.

Here is how to take a web page url on your own website, grab the html code from that web page url, convert the html code into a document and then use Javascripts .querySelectorAll() function to extract any html elements you wish. In this particular example we are extracting the links from the document created from the html code grabbed from a web page url.

3 Steps Required to Get All Links from a Web Adress/URL

  1. Ajax to fetch the html from the web address/url.
  2. Convert the Ajax returned html code into a document which can be processed by Javascript code.
  3. Extract whatever elements you wish using the Javascript .querySelectorAll() function.
S
H
A
R
E