Search code examples
javascriptgoogle-chromeinternet-explorerbrowser-detectionbrowser-feature-detection

How do I detect if a browser can open Network Folder inside iframe?


I need to alert users browsing with Chrome that they need to open a page using IE, as it allow open a Network Folder inside iframe.

But I don't want to detect browser, is there a way to detect this feature?


Solution

  • No, you cannot detect this feature (if you can call it that, allowing web pages to display local folders in a frame is a very bad idea). The same-origin policy prevents you from knowing what was loaded into the frame so you cannot distinguish a network folder from an error page. Best you can do is checking how long the frame takes to load:

    var start = new Date().getTime();
    var frame = document.createElement("iframe");
    frame.addEventListener("load", function()
    {
      if (new Date().getTime() - start < 100)
        alert("Looks like the network folder didn't load, try using a less secure browser?");
    }, false);
    frame.src = "file://mynetworkpath/folder";
    document.body.appendChild(frame);
    

    In my tests it took Chrome around 30 milliseconds to load the error page, the code above sets 100 milliseconds as threshold just to be sure. Loading a network folder should always take considerably longer. This code didn't work in Firefox 30, for some reason the load event isn't fired there. This might be a security precaution but I don't really know that.

    Alternatively, you can try loading an image that is known to exist:

    var img = new Image();
    img.onload = function()
    {
      // Can display the frame
    };
    img.onerror = function()
    {
        alert("Try using a less secure browser?");
    };
    img.src = "file://mynetworkpath/folder/pixel.gif";
    

    Note that neither solution will allow distinguishing between "browser won't let me access network folders" and "network folder is inaccessible, probably no network connection."