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);

// }


API Connection Check if Live or Not

 const token = $v('P12_TOKEN');

fetch('https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata', {

    method: 'POST',

    headers: {

            'Authorization': `Bearer ${token}`,

            'Content-Type': 'application/json'

        },

    body: JSON.stringify({

        // Dummy data for API Testing

        test: true

    })

})

.then(response => {

    // Agar server response de raha hai (chahe 200 ho ya 400 Bad Request)

    console.log('Status Code:', response.status);

    return response.json().catch(() => ({})); //  Safe Fallback if JSON Not Available

})

.then(data => {

    console.log('API Response:', data);

    apex.message.alert('API is Live and Responding!');

})

.catch(error => {

    // This will Run if  network or  API is down/unreachable

    apex.message.alert('Alert: FBR API Not Responding / Network Error..!');

    console.error('Error:', error);

});

Check Internet Connectivity on Local System

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