Skip to content

Instantly share code, notes, and snippets.

@kyya
Forked from jbinto/getTitleNative.js
Last active January 9, 2023 18:09
Show Gist options
  • Save kyya/d63c2cad60f49fa0c9abba4f4d7bc922 to your computer and use it in GitHub Desktop.
Save kyya/d63c2cad60f49fa0c9abba4f4d7bc922 to your computer and use it in GitHub Desktop.
Get title from remote HTML URL - without jQuery
// Only using native browser features (no jQuery).
// Uses `fetch`, `DOMParser` and `querySelectorAll`.
const getTitle = (url) => {
return fetch(`${url}`)
.then((response) => response.text())
.then((html) => {
const doc = new DOMParser().parseFromString(html, "text/html");
const title = doc.querySelectorAll('title')[0];
return title.innerText;
});
};
var urls = [
'https://www.h404bi.com/blog/2019/07/better-lazyload-implementation-with-responsive-images',
'https://baidu.com'
]
// This one keeps the order the same as the URL list.
Promise.all(
urls.map((url) => getTitle(url))
).then((titles) => {
console.log(titles);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment