JavaScript detection/redirection if an intranet site is accessible

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, acts as the corporate intranet site that is not accessible from outside the company’s network, and // 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 // 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="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Intranet Detection Script</title>
<script type="text/javascript">

var internalURL = '';
var publicURL = '//';
var detectionCounter = 0;
var detectionTimeOut = 5;
var detectionImage = '' + (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 () {



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.

3 thoughts on “JavaScript detection/redirection if an intranet site is accessible

  1. bob

    This script is great, thank you for posting it!
    I did find one mistake though, which took me a minute to figure out. The double forward slashes on line 34 should go on line 33 instead. Once I corrected that, the script runs perfectly. The other issue I’m trying to overcome now is that upon page load, IE keeps prompting before allowing the script to run. I’d probably have to relax the browser security settings, but I rather not do that for the entire user base.

  2. bob

    Forget what I said about the IE security prompt. It was only happening when I was testing the html page locally. Once uploaded to the web server, it ran seamlessly. Again many thanks!

Comments are closed.