I am getting the error:
script.js:11 Uncaught TypeError: Cannot read properties of undefined (reading 'includes')
And I don’t understand why. Is the body
const incorrect?
How can I fix this?
const checkForText = (url, text) => {
const page = fetch(url, { mode: 'no-cors' });
const body = page.text;
if (body.includes(text)) {
console.log(`Passed. "${text}" found on ${url}`);
} else {
console.error(`Failed. Did not find "${text}" on ${url}!`);
}
}
checkForText('https://dsbelmex.biz/nosotros/', 'equilibrio');`
I tried a lot of different things, but nothing worked. I’m still learning JavaScript so I’m quite the beginner.
2
Answers
I wouldn’t recommend to try to access external websites directly or interact with the web in real-time to perform actions like searching a webpage for specific content, unless it is in your own server.
However, I am providing you with a JavaScript function that you can use in your own web development project to search for a specific word on a webpage using JavaScript. You’ll need to run this code within a browser’s developer console or in a JavaScript environment to see the results. Good Luck!
Example screenshot tested on my browser: