Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Created May 19, 2016 13:03
Show Gist options
  • Save hirokazumiyaji/d94b71ee7e19acfded60ed885b5f57c3 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/d94b71ee7e19acfded60ed885b5f57c3 to your computer and use it in GitHub Desktop.
Javascript Title Get
function callback(title) {
console.log(title);
}
function getTitle(url, callback) {
var title = '';
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
var end = false;
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var htmlText = xhr.responseText;
title = htmlText.slice(htmlText.indexOf('<title>'), + 7, htmlText.indexOf('</title>'));
callback(title);
}
}
xhr.send(null);
}
getTitle('https://www.google.com', callback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment