Skip to content

Instantly share code, notes, and snippets.

@DNA
Created October 20, 2017 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DNA/2192ea69ade1c6c199c8069536011225 to your computer and use it in GitHub Desktop.
Save DNA/2192ea69ade1c6c199c8069536011225 to your computer and use it in GitHub Desktop.
check if url is available
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
let xhr = new XMLHttpRequest();
let serverStatus = document.getElementById('serverStatus');
xhr.open('HEAD', 'https://api.github.com/');
xhr.timeout = 2000;
xhr.onload = _ => serverStatus.innerText = 'ONLINE';
xhr.onerror = _ => serverStatus.innerText = 'OFFLINE';
xhr.ontimeout = _ => serverStatus.innerText = 'OFFLINE';
xhr.send();
}
};
</script>
</head>
<body>
Server status: <span id="serverStatus">checking...</span>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment