Skip to content

Instantly share code, notes, and snippets.

@jpsilvashy
Forked from louisremi/serverReachable.js
Last active November 10, 2020 09:59
Show Gist options
  • Select an option

  • Save jpsilvashy/5725579 to your computer and use it in GitHub Desktop.

Select an option

Save jpsilvashy/5725579 to your computer and use it in GitHub Desktop.

Revisions

  1. jpsilvashy revised this gist Jun 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hostReachable.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ function hostReachable() {
    var status;

    // Open new request as a HEAD to the root hostname with a random param to bust the cache
    xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.random(), false );
    xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false );

    // Issue request and handle response
    try {
  2. jpsilvashy revised this gist Jun 6, 2013. 2 changed files with 18 additions and 23 deletions.
    18 changes: 18 additions & 0 deletions hostReachable.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    function hostReachable() {

    // Handle IE and more capable browsers
    var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );
    var status;

    // Open new request as a HEAD to the root hostname with a random param to bust the cache
    xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.random(), false );

    // Issue request and handle response
    try {
    xhr.send();
    return ( xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 );
    } catch (error) {
    return false;
    }

    }
    23 changes: 0 additions & 23 deletions serverReachable.js
    Original file line number Diff line number Diff line change
    @@ -1,23 +0,0 @@
    function serverReachable() {
    // IE vs. standard XHR creation
    var x = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" ),
    s;
    x.open(
    // requesting the headers is faster, and just enough
    "HEAD",
    // append a random string to the current hostname,
    // to make sure we're not hitting the cache
    "//" + window.location.hostname + "/?rand=" + Math.random(),
    // make a synchronous request
    false
    );
    try {
    x.send();
    s = x.status;
    // Make sure the server is reachable
    return ( s >= 200 && s < 300 || s === 304 );
    // catch network & other problems
    } catch (e) {
    return false;
    }
    }
  3. Louis-Rémi Babé revised this gist Apr 22, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion serverReachable.js
    Original file line number Diff line number Diff line change
    @@ -16,8 +16,8 @@ function serverReachable() {
    s = x.status;
    // Make sure the server is reachable
    return ( s >= 200 && s < 300 || s === 304 );
    // catch network & other problems
    } catch (e) {
    // throws NETWORK_ERR when... there is an problem with the network.
    return false;
    }
    }
  4. Louis-Rémi Babé revised this gist Apr 22, 2011. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions serverReachable.js
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,9 @@ function serverReachable() {
    x.open(
    // requesting the headers is faster, and just enough
    "HEAD",
    // append a random string to the current hostname, to make sure we're not hitting the cache
    "//" + window.location.hostname + "/?rand=" + ( Math.random() * 1E9 ),
    // append a random string to the current hostname,
    // to make sure we're not hitting the cache
    "//" + window.location.hostname + "/?rand=" + Math.random(),
    // make a synchronous request
    false
    );
  5. Louis-Rémi Babé revised this gist Apr 22, 2011. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion serverReachable.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,22 @@
    function serverReachable() {
    // IE vs. standard XHR creation
    var x = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" ),
    s;
    x.open( "HEAD" , "//" + window.location.hostname + "/?rand=" + ( Math.random() * 1E9 ), 0);
    x.open(
    // requesting the headers is faster, and just enough
    "HEAD",
    // append a random string to the current hostname, to make sure we're not hitting the cache
    "//" + window.location.hostname + "/?rand=" + ( Math.random() * 1E9 ),
    // make a synchronous request
    false
    );
    try {
    x.send();
    s = x.status;
    // Make sure the server is reachable
    return ( s >= 200 && s < 300 || s === 304 );
    } catch (e) {
    // throws NETWORK_ERR when... there is an problem with the network.
    return false;
    }
    }
  6. Louis-Rémi Babé created this gist Apr 22, 2011.
    12 changes: 12 additions & 0 deletions serverReachable.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    function serverReachable() {
    var x = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" ),
    s;
    x.open( "HEAD" , "//" + window.location.hostname + "/?rand=" + ( Math.random() * 1E9 ), 0);
    try {
    x.send();
    s = x.status;
    return ( s >= 200 && s < 300 || s === 304 );
    } catch (e) {
    return false;
    }
    }