[ROYALINE’s] SharePoint Dynamics

SharePoint Development & Techniques

How to display an error if an unsupported browser views your SharePoint site

Posted by Freelance-Puma on August 11, 2011


When I create web parts for my SharePoint sites, I try to make it a good practice to detect what browser is viewing my site and then display a message if it is not supported.

For instance, the company I work for only supports Internet Explorer. I am able to use other browsers in my organization but the content on any web page is only guaranteed to be displayed correctly within IE. All other browsers may or may not display the information correctly, so IE is the only option I have to work with.

So, I often use Content Editor Web Parts to display content on my site, especially if I am using javascript to query my SharePoint sites for data (using the SharePoint web services). However, if a user goes to one of my sites and gets errors or has improperly formatted data, I have to instruct them on the company’s policy to use IE only to view the site. In most cases, this doesn’t go over well.

To use this code example, add the code to a Content Editor Web Part on your site (I usually put it at the top):

<script language="javascript">
    //DETECT BROWSER - FIREFOX IS NOT SUPPORTED
    var BROWSER_AGENT = navigator.userAgent.toLowerCase();
    if (BROWSER_AGENT.indexOf("msie") != -1) {
        // *** BROWSER IS SUPPORTED: DO WORK HERE ***
    } else {
        browserNotSupported();
    }
    function browserNotSupported() {
        var err = "";
        err += "<tr>";
        err += "<td class='ms-informationbar' style='padding: 2px;' valign='middle'>";
        err += "<table width='100%' border='0' cellspacing='0' cellpadding='0'>";
        err += "    <tr>";
        err += "        <td class='ms-descriptiontext' style='padding: 2px; width: 18px; vertical-align: top;'>";
        err += "            <img src='/_layouts/images/ewr238m.GIF' />";
        err += "        </td>";
        err += "        <td class='ms-descriptiontext' style='padding: 2px; vertical-align: top;'>";
        err += "             Content in this area requires Internet Explorer. Please view this page in Internet Explorer only.";
        err += "        </td>";
        err += "    </tr>";
        err += "</table>";
        err += "</td>";
        err += "</tr>";
        document.write(err);
    }
    //END BROWSER CHECK
</script>

When a user goes to the site and is using Firefox or another non-IE browser, they will be presented with the following error:

If this site is viewed with Internet Explorer, no messages are displayed and everything looks good.

END

Leave a comment