Home > Net >  How to search through an html file from an external link using JavaScript?
How to search through an html file from an external link using JavaScript?

Time:12-02

I'm very new to Javascript and coding in general and have a project I would really like to work on, but am having trouble knowing where to start, in particular step one. So far this is what I want the programme to do:

  1. Access/read html files from four external links (Is this where I use fetch()? Are there any packages you can recommend for this?)

  2. User inputs a list of names

  3. Search html files for each item in list

4a. If there is a match, then output 'match' and which file the match was found

4b. If no match then output 'no match'

Is it more complex than just using HTML, CSS and JavaScript?

Thank you in advance!

CodePudding user response:

You won't be able to get data from external url in browser due to CORS policy

What you should do instead is create server-side solution and API for it.

Server side solution should request the external page HTML code, search for the word and send the result back to the browser.

CodePudding user response:

The problem with implementing this in the browser is because of cross-origin issues. Essentially you aren't allowed to fetch() nor XMLHttpRequest() to other domains unless they specifically let you to. See the mdn docs here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

You could implement this is Node.js which allows you to run js in a desktop environment. If you were doing so, i recommend the following:

  • use node-fetch to do HTTP requests
  • use cheerio to parse the data

Best Regards, Kelvin

  • Related