我需要提醒使用 Chrome 浏览的用户他们需要使用 IE 打开页面,因为它允许在 iframe 中打开网络文件夹。
但是我不想检测浏览器,有没有办法检测这个功能?
最佳答案
不,您无法检测到此功能(如果您可以这样调用它,那么允许网页在框架中显示本地文件夹是一个非常糟糕的主意)。同源策略使您无法知道加载到框架中的内容,因此您无法区分网络文件夹和错误页面。您能做的最好的事情就是检查加载框架需要多长时间:
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);
在我的测试中,Chrome 需要大约 30 毫秒才能加载错误页面,上面的代码将 100 毫秒设置为阈值只是为了确定。加载网络文件夹应该总是花费相当长的时间。此代码在 Firefox 30 中不起作用,由于某种原因,load
事件未在此处触发。这可能是一种安全预防措施,但我真的不知道。
或者,您可以尝试加载已知存在的图像:
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";
请注意,这两种解决方案都不允许区分“浏览器不允许我访问网络文件夹”和“网络文件夹不可访问,可能没有网络连接。”
关于javascript - 如何检测浏览器是否可以在 iframe 中打开网络文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24205543/