Created
May 19, 2016 13:03
-
-
Save hirokazumiyaji/d94b71ee7e19acfded60ed885b5f57c3 to your computer and use it in GitHub Desktop.
Javascript Title Get
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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