如何使一个由其他人编写的 jquery 插件在所有它实际上可以工作的浏览器中工作?
如何使用功能检测来制作由其他人编写的插件,以便在所有可以实际运行/受支持的浏览器上运行。 插件的文档说它在 IE 8 中受支持,但实际上并不起作用。我知道这是因为浏览器检测不可靠,但是我该怎么做才能使它在所有可以实际工作的浏览器中工作?
详细信息
举一个具体的例子,我使用的是 jQuery 选择的插件版本 0.9.7,正如文档所述 -
支持所有现代浏览器(Firefox、Chrome、Safari 和 IE9)。 还启用了对 IE8 的旧版支持。
但是,我发现它在许多 IE 8 中不起作用,但在 IE 测试器 IE 8 上起作用。然后在插件代码中,我发现了这个浏览器检测逻辑,它只是从插件逻辑中逃脱 -
if ($.browser.msie && ($.browser.version === "6.0" || $.browser.version === "7.0")) {
return this;
}
我发现 $. browser.version
在我的 IE 8 上返回 7.0,这就是它不起作用的原因。嗯,我知道,没什么值得惊讶的。我尝试删除 $.browser.version === "7.0"
条件,只是为了检查它是否确实适用于我的 IE 8 并且它有效。但是,我无法删除该条件,因为该插件的作者已经这样做了,而且他一定更了解。
现在,我的问题是如何使用功能检测使其在所有可以实际工作的浏览器上工作。我想合乎逻辑的步骤是找出该插件使用了哪些所有浏览器功能。那我怎样才能找到呢?请原谅我对此的了解有限。我只是了解特征检测的理论知识,它如何比浏览器检测更好等,但从未实际使用过任何进行特征检测的代码。我大约一两年前就知道了这一点。
而这次当我搜索时,我了解到了Modernizer。这有帮助吗?我不确定,因为它说它是为了构建下一代 HTML5 和 CSS3 驱动的网站,而我们还不太使用 HTML5、CSS3。或者,一旦我知道插件需要哪些功能,jQuery.support 对我来说就足够了。
一些进一步的推理
我发现了这个问题通过对象/功能进行浏览器检测检测,作者询问是否可以使用特征检测来弄清楚浏览器到底是什么。一条评论说:“功能检测的全部意义在于,您可以查明浏览器是否确实具有您需要的功能。当您正确执行此操作时,您实际上并不关心它是什么浏览器,您只关心它是否具有具有您完成工作所需的功能。”我理解并同意这一点,但是说这些浏览器支持给定的插件有什么意义呢?为什么插件作者不列出所有需要的功能,从而方便人们使用?但是,在我看来,列出支持的浏览器背后的要点只是通过假设理想的浏览器来提供兼容性的想法。
例如,两个理想的 IE 6 浏览器将具有相同的选择下拉 z-index 问题,干扰弹出 div。假设某个插件存在此问题,那么说该插件与 IE 6 不兼容意味着这是一个理想的 IE 6 浏览器,而不是另一个用户代理经过调整并返回 IE 6 作为浏览器版本的 IE 8 浏览器。
在这种情况下(列出支持的浏览器背后的要点只是通过假设理想的浏览器来提供兼容性的想法),使用功能检测来识别浏览器是否有意义?如果,那么为什么没有太多可用的示例片段?
很多问题..:)我想,今天我问了所有......
How do I use feature detection to make a plugin, which was written by someone else, to work on all those browsers where it could actually work/is supported. The plugin's documentation says it is supported in IE 8 but it doesn't actually work. I know it is because of the unreliable browser detection but what can I do to make it work in all those browsers where it could actually work?
Details
Just to give a particular example, I am using jQuery chosen plugin version 0.9.7 and as the documentation says -
All modern browsers are supported (Firefox, Chrome, Safari and IE9).
Legacy support for IE8 is also enabled.
However, I found that it does not work in many IE 8s but works on IE tester IE 8. Then inside the plugin code I found this browser detection logic which simply escapes out of the plugin logic -
if ($.browser.msie && ($.browser.version === "6.0" || $.browser.version === "7.0")) {
return this;
}
And I found that $.browser.version
was returning 7.0 on my IE 8 and that's why it was not working. Well, nothing to be surprised of, I know. I tried removing the $.browser.version === "7.0"
condition, just to check if it actually works on my IE 8 and it worked. But, I cannot remove that condition since the author of the plugin has done that and he must be knowing better.
Now, my question is how do I use feature detection to make it work on all those browsers it can actually work. I guess the logical step would be to find out which all browser features are used by this plugin. Then how do I find that out? Please pardon my limited knowledge regarding this. I just have theoretical knowledge of feature detection, how it is better than browser detection etc. but never actually used any code which does feature detection. I learned about this some 1-2 years ago.
And this time when I searched, I came to know about Modernizer. Could this be helpful? I am not sure because it says it for building the next generation of HTML5 and CSS3-powered websites and we don't use HTML5, CSS3 much yet. Or is jQuery.support enough for me once I know which all features are needed by the plugin.
Some further Reasoning
I found this question Browser detection via object/feature detection where the author asks whether it is possible to use feature detection to figure out what the browser actually is. One comment says - "The whole point of feature detection is so that you can find out whether the browser actually has the features you need. When you do that right, you don't actually care what browser it is, you just care whether it has the feature you need to do your job." I understand and agree to this, but then what is the point in saying that a given plugin is supported in these browsers. Why don't plugin authors list out all the features that are needed, thereby making it easy for people to use? But, then it looks for me like the point behind listing the supported browsers is just giving an idea of the compatibility by assuming ideal browsers.
e.g., two ideal IE 6 browsers will have the same select drop-down z-index issue interfering with popup divs. Suppose, a plugin has issues with this, then saying this plugin is not compatible with IE 6 means an ideal IE 6 browser and not another IE 8 browser whose user agent is tempered with and which returns IE 6 as the browser version.
In that case (point behind listing the supported browsers is just giving an idea of the compatibility by assuming ideal browsers), does it make sense to identify browsers using feature detection? If, so why aren't there much example snippets available?
Lots of questions.. :) I guess, today I asked it all...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试在插件中或在使用插件之前执行此操作:
示例:http://jsbin.com/inisol
顺便说一句:没有 IE 来测试它。希望它有效。
You may try to execute this in a plugin or before You use the plugin:
Example: http://jsbin.com/inisol
Btw: Don't have IE to test it. Hope it works.