Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML redirect to different URL based on server availability

The default home page for our company workstations is http://intranet, which is our internal SharePoint site set by group policy. Right now, if a user attempts to open IE on a laptop when they are off-site, they are (obviously) greeted by a "Page cannot be displayed" error. This causes confusion from our less sophisticated users and they wind up calling our help desk even though there is nothing wrong with their internet connection.

What I would like to do is set the default home page to a local .html file that will use an HTTP redirect to forward the browser to our public web site if the internal URL is not reachable.

Is this possible?

like image 767
Wes Sayeed Avatar asked Oct 18 '22 18:10

Wes Sayeed


1 Answers

All too often, something that seems easy to implement can turn out to be quite challenging. In this case, JavaScript prohibits cross-domain calls for security measures, so a XMLHttpRequest isn't an option.

It seems like your best option would be to implement the solution discussed here: Test url availability with javascript.

I did some quick testing in Chrome & IE and this code worked well in both. (IE did complain about running the script on a local page, but this would be the same regardless of solution.)

<html>
<head></head>
<body>
<script>
function checkServerStatus(url)
{
    var script = document.body.appendChild(document.createElement("script"));
    script.onload = function()
    {
        alert( url + " is online.");
    };
    script.onerror = function()
    {
        alert( url + " is offline.");
	window.location.replace("http://google.com");
    };
    script.src = url;
}
checkServerStatus("http://google.com");
checkServerStatus("http://intranet");
</script>
</body>

Here's another link that discussing this solution: https://petermolnar.eu/test-site-javascript/.

Hope this helps.

like image 119
Josh Greenberg Avatar answered Oct 27 '22 10:10

Josh Greenberg