Wednesday, 7 October 2026

Check Internet Connectivity on Local System



if (!navigator.onLine) {

    showOfflineAlert();

} else {

    // Set a strict 3-second network timeout threshold

    const controller = new AbortController();

    const timeoutId = setTimeout(() => controller.abort(), 3000); 


    fetch("https://google.com", { 

        method: 'HEAD', 

        cache: 'no-store', 

        mode: 'no-cors',

        signal: controller.signal 

    })

    .then(function() {

        clearTimeout(timeoutId); // Cancel the timeout on success

        console.log("Local system internet connectivity confirmed.");

        showOnlineAlert();

    })

    .catch(function() {

        clearTimeout(timeoutId); // Clean up

        showOfflineAlert();

    });

}


function showOnlineAlert() {

    var alertOptions = {

        title: "Internet Connected",

        style: "success",                                

        iconClasses: "fa fa-check-circle fa-2x",         

        okLabel: "Dismiss"                                 

    };

    apex.message.alert("Your local system is actively connected to the internet.", function() {}, alertOptions);

}


function showOfflineAlert() {

    var alertOptions = {

        title: "No Internet Connection",

        style: "danger",                                 

        iconClasses: "fa fa-exclamation-triangle fa-2x", 

        okLabel: "Retry"                                 

    };

    apex.message.alert("Your local system has lost internet access. Please check your network cables or Wi-Fi.", function() {}, alertOptions);

}





// if (!navigator.onLine) {

//     showOfflineAlert();

// } else {

//     // Ping an open public endpoint designed to return an empty 204 status without CORS blocks

//     fetch("https://google.com", { method: 'HEAD', cache: 'no-store', mode: 'no-cors' })

//         .then(function() {

//             // A successful handshake (even opaque) proves local internet can reach the outside world

//             console.log("Local system internet connectivity confirmed.");

//             showOnlineAlert();

//         })

//         .catch(function() {

//             showOfflineAlert();

//         });

// }


// function showOnlineAlert() {

//     var alertOptions1 = {

//         title: "Internet Connected",

//         style: "success",                                

//         iconClasses: "fa fa-check-circle fa-2x",         

//         okLabel: "Dismiss"                                 

//     };

//     apex.message.alert("Your local system is actively connected to the internet.", function() {}, alertOptions1);

// }


// function showOfflineAlert() {

//     var alertOptions = {

//         title: "No Internet Connection",

//         style: "danger",                                 

//         iconClasses: "fa fa-exclamation-triangle fa-2x", 

//         okLabel: "Retry"                                 

//     };

//     apex.message.alert("Your local system has lost internet access. Please check your network cables or Wi-Fi.", function() {}, alertOptions);

// }


No comments:

Post a Comment

Check Internet Connectivity on Local System

if (!navigator.onLine) {     showOfflineAlert(); } else {     // Set a strict 3-second network timeout threshold     const controller = new ...