Search code examples
javascripthttp-redirectofflinestart-page

Redirect only if user is online


I am currently having an Issue with a Clients Intranet. We used to have a start-site where some js checks, wether the user is online or not, and if so that he is redirected to the home-page of our Intranet. Now I am testing the compatibility with the new Microsoft EDGE and found out that the automatic redirect isn´t working anymore. Do you have any ideas of how to check if the user has a valid internet connection and then redirect?

Current solution is:

var myImg = new Image();
    myImg.src = "https://URL_of_our_intranet/images/blank.gif";
    myImg.onload = myImgOnLoadHandler;

    function myImgOnLoadHandler(e) {
        window.location =  "redirect-URL";
    }

The whole Code is on each Users Local System and there is no JQuery-Solution possible as there is no JQ-Library available. I would love to hear an easy and on EDGE working solution.

If there is an easy way to do so, please let me know - I am not a pro in Javascript..

Thanks! Sam


Solution

  • The solution that perfectly worked for me, as we only use Internet Explorer (at least some use firefox, but Opera is not available on any client):

    if (navigator.onLine) {
        window.location.replace("https://www.google.de");
        } 
    

    Thanks for your quick help!