Here’s an example script demonstrating how a publicly accessible home page can leverage JavaScript to detect whether a machine is on a corporate intranet and then redirect the browser to an intranet page.
In the example, http://alephstudios.com acts as the corporate intranet site that is not accessible from outside the company’s network, and //ardamis.com acts as the publicly accessible site, which can be accessed both from within and outside the corporate network.
The browser is set to use a page on the public //ardamis.com site which includes some JavaScript that attempts to load an image from a location on the company intranet. If the image can be successfully loaded by the browser, we have establishe that the machine is on the internal network. The browser can then be redirected via JavaScript to an appropriate intranet page. Otherwise, the browser is redirected to an Internet page.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Intranet Detection Script</title> <script type="text/javascript"> <!-- var internalURL = 'http://alephstudios.com'; var publicURL = '//ardamis.com'; var detectionCounter = 0; var detectionTimeOut = 5; var detectionImage = 'http://alephstudios.com/testing/intranet/transparent.gif?' + (new Date()).getTime(); var detectionElement = document.createElement('img'); detectionElement.src = detectionImage; function detectIntranet() { detectionCounter = detectionCounter + 1; // alert('Attempt ' + detectionCounter + ': Sniffing intranet connection by loading an internal resource at ' + detectionImage); if (detectionElement.complete) { if (detectionElement.width > 0 && detectionElement.height > 0) { // alert('Attempt ' + detectionCounter + ': The intranet resource was loaded!'); window.location = internalURL; } else { // alert('Attempt ' + detectionCounter + ': The intranet resource could not be loaded!'); window.location = publicURL; } } else { if (detectionCounter < detectionTimeOut) { setTimeout("detectIntranet()", 1000); // alert('Attempt ' + detectionCounter + ': Still trying to load: ' + detectionImage); } else { alert('Attempt ' + detectionCounter + ': Gave up trying to load: ' + detectionImage); // window.location = publicURL; } } } window.onload = function () { detectIntranet(); } //--> </script> </head> <body> </body> </html>
Setting up an intranet detection/redirection page as the browser’s home page allows IT to display an intranet page while the device is on the network and an Internet page when the device is off the network.