在研究IE的JavaScript条件评论时,我偶然发现了@cc_on.这似乎有效.但是,条件注释的维基百科条目为更强大的IE检测提供了以下代码,特别是IE6:
/*@cc_on
@if (@_jscript_version > 5.7)
document.write("You are using IE8+");
@elif (@_jscript_version == 5.7 && window.XMLHttpRequest)
document.write("You are using IE7");
@elif (@_jscript_version == 5.6 || (@_jscript_version == 5.7 && !window.XMLHttpRequest))
document.write("You are using IE6");
@elif (@_jscript_version == 5.5)
document.write("You are using IE5.5");
@else
document.write("You are using IE5 or older");
@end
@*/
Run Code Online (Sandbox Code Playgroud)
问题是,我得到一个"预期的常量"javascript错误!window.XMLHttpRequest.
很明显,维基百科需要一些帮助,我需要让它发挥作用.谁能帮我吗?
javascript internet-explorer conditional-comments internet-explorer-6 browser-feature-detection
在所有浏览器中,似乎只有Opera不支持onunload/onbeforeunload事件.(已经十五年了,Opera!)这个问题的解决方案已被多次覆盖,例如:onbeforeunload支持检测
不幸的是,从Opera 11.51开始,("onbeforeunload" in window) == true但实际的onbeforeunload事件从未执行过!
我的Web应用程序需要在用户离开页面时将数据发送到服务器; 我正在使用同步ajax请求.看起来我不得不求助于在页面某处使用"保存"按钮来掩盖Opera问题.但是,我不希望这个按钮混淆浏览器能够通过ajax自动保存的用户,所以我真的很喜欢这个按钮只显示在Opera中.
是我唯一的选择浏览器检测?问题是,Opera可以选择将自己伪装成其他浏览器.
javascript opera onbeforeunload onunload browser-feature-detection
在JavaScript中有很多方法可以检测浏览器.
据我所知,使用navigator.userAgent或检测功能(如XMLHttpRequest)等.
谁能告诉我哪种方式最好也最有效?
Internet Explorer不支持iframe网址的数据uri方案(请参阅http://msdn.microsoft.com/en-us/library/cc848897%28v=vs.85%29.aspx).其他浏览器呢.由于浏览器检测加载了测试和面向未来的问题,我想使用特征检测来解决此问题.
那么:如何检测浏览器是否支持iframe的数据uri方案?
javascript iframe cross-browser data-uri-scheme browser-feature-detection
我编写了一个脚本来测试IMG标记中的SVG支持:
function SVGinIMG() {
var SVGdata = 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNzUiIGhlaWdodD0iMjc1Ij48L3N2Zz4%3D'
var i = document.createElement('img');
i.setAttribute('src',SVGdata);
return i.complete;
}
window.onload = function() {
var hasSVG = SVGinIMG();
alert(hasSVG);
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要的,除了当我在WebKit浏览器中运行脚本时,我第一次加载页面时不会触发complete属性; 当我刷新页面时,它可以正常工作.在返回的IMG已完成加载之前函数运行; 推迟这个的最好方法是什么?
我想只在apple iphone设备中更改一些CSS属性.有没有办法找到单独使用iPhone.
对于所有的狡猾
.menu{
-webkit-transition:all 100ms ease;
-moz-transition:all 100ms ease;
-o-transition:all 100ms ease;
transition: all 100ms ease;
}
Run Code Online (Sandbox Code Playgroud)
仅适用于Apple Iphone
.menu{
transition: none;
}
Run Code Online (Sandbox Code Playgroud) 我需要检测IE6才能解决缺少位置的问题:修复.我一直在使用一个简单的正则表达式:
var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
Run Code Online (Sandbox Code Playgroud)
除了浏览器声称同时属于IE6和IE7的用户外,这几乎一直都有效:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)
Run Code Online (Sandbox Code Playgroud)
辉煌.
我喜欢使用jquery.support,但看起来它不支持查询position:fixed是否可用.所以我回来检测IE6.
有各种建议的解决方案,例如寻找maxHeight的存在.但那些似乎相当随意并吓到我 - 如果上面的正则表达式有例外,我怎么能确定maxHeight没有例外?
我正在考虑使用条件评论 - 至少它是IE本身声称是IE6,而不是黑客.就像是:
<!--[if IE 6]>
<SCRIPT> var isIE6 = true; </SCRIPT>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)
或者,可以直接测试position:fixed是否可用,但这看起来有点沉重.
我的条件评论方法不起作用的任何原因?有更好的方法吗?
javascript css jquery internet-explorer-6 browser-feature-detection
随着jQuery 2.0的发布,已经有很多关于如何识别用户是否正在使用支持它的IE版本的讨论(jQuery 2.0仅支持IE9及更高版本).
我的问题是,为什么像一个解决方案这样:
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
Run Code Online (Sandbox Code Playgroud)
比查看navigator对象更受欢迎:
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure. …Run Code Online (Sandbox Code Playgroud) javascript internet-explorer cross-browser browser-feature-detection
我想检查浏览器是否支持 .textContent
我想过这些选择:
if( document.body.textContent ) { }if( document.createElement('div').textContent !== void(0) ) { }当然,第一个更简单,但我看到的问题是浏览器可能计算所有字符串,如果网站很大,这可能会很慢.
然后,我应该选择哪一个?或者有更好的选择吗?
编辑:我创建了一个jsperf
我正在开发一款支持Android Chrome和iOS Safari中"添加到主屏幕"功能的应用.由于我希望对这两个功能都提供通用脱机支持,但我只想使用我必须的清单文件,以便增加我的控制权.但是,iOS Safari不支持服务工作者,所以我的问题是如果不存在对Service Workers的支持,我怎么才能实例化缓存清单文件,更具体地说; 我知道我可以添加manifest='whatever.appcache'到<html>用JavaScript代码,而是将浏览器,例如iOS的Safari浏览器,使用缓存?
html5 offline-caching cache-manifest service-worker browser-feature-detection
javascript ×9
css ×2
html5 ×2
dom ×1
iframe ×1
jquery ×1
onunload ×1
opera ×1
performance ×1
svg ×1