make a fetch request to html page code example
Example 1: fetch html from url
fetch('/about').then(function (response) {
return response.text();
}).then(function (html) {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
var img = doc.querySelector('img');
console.log(img);
}).catch(function (err) {
console.warn('Something went wrong.', err);
});
Example 2: how to fetch web page source code with javascript
var myLoc = window.prompt('Please enter a web site address', 'http://');
getHttp = myLoc.substring(0, 7);
if ( getHttp == "http://")
{
finalUrl = myLoc;
} else {
finalUrl = 'http://' + myLoc;
}
window.location = 'view-source:' + finalUrl;