admin管理员组文章数量:1432378
I can't understand this code. If this is a RegExp, can it be done in a simpler way? Or is this already widely patible? (with IE6 and newer browsers)
var u = navigator.userAgent;
// Webkit - Safari
if(/webkit/i.test(u)){
// Gecko - Firefox, Opera
}else if((/mozilla/i.test(u)&&!/(pati)/.test(u)) || (/opera/i.test(u))){
}
Is this just:
String.indexOf("webkit")
I can't understand this code. If this is a RegExp, can it be done in a simpler way? Or is this already widely patible? (with IE6 and newer browsers)
var u = navigator.userAgent;
// Webkit - Safari
if(/webkit/i.test(u)){
// Gecko - Firefox, Opera
}else if((/mozilla/i.test(u)&&!/(pati)/.test(u)) || (/opera/i.test(u))){
}
Is this just:
String.indexOf("webkit")
Share
Improve this question
edited May 16, 2009 at 9:17
Robin Rodricks
asked May 16, 2009 at 9:12
Robin RodricksRobin Rodricks
114k147 gold badges414 silver badges617 bronze badges
4
- I also have a local variable called "i". Is the 'i' in the above code referring to that? – Robin Rodricks Commented May 16, 2009 at 9:16
-
1
The i’s in
/…/i
is the regex case-insensitive modifier. – Gumbo Commented May 16, 2009 at 9:17 - But that needs to be within a string. – Ólafur Waage Commented May 16, 2009 at 9:18
-
String.indexOf would give different result even without being strict (===):
"webkit".indexOf("webkit") == /webkit/i.test("webkit")
is false in fact indexOf gives 0.... thus maybe, if we just forget about performances, it could be pared toyourString.indexOf("webkit")>=0
– fedeghe Commented May 1, 2016 at 14:43
5 Answers
Reset to default 7First it looks for "webkit" (ignoring case) in the string u
in an attempt to determine that the browser is Safari.
If it doesn't find that, it looks for "mozilla" (without "pati") or "opera" in an attempt to determine that the browser is Firefox or Opera. Again, the searches are ignoring case (/i
).
EDIT
The /.../i.test()
code is a regular expression, these are built into JavaScript.
test() in javascript is a regular expression test function. You can read more about it here.
This method tests for a match of a regular expression in a string, returning true if successful, and false if not. The test method can be used with either a string literal or a string variable.
Code:
rexp = /er/
if(rexp.test("the fisherman"))
document.write("It's true, I tell you.")
Output:
It's true, I tell you.
Also here's another great page that goes into more detail on this function.
Executes the search for a match between a regular expression and a specified string. Returns true or false.
This is similar, but returns a client name and version for any browser.
window.navigator.sayswho= (function(){
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie)\/? *(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
M= M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];
return M;
})();
alert(navigator.sayswho)
You code seems to be some kind of browser sniffing. The value of u
is probalby the user agent identifier. And it’s tested with regular expressions (build using the RegExp literal syntax /
expr
/
).
Method test of the regular expressions tests for a match of a regular expression in a string, returning true if successful, and false if not.
this code tests for a match of the regular expressions 'webkit', 'mozilla' etc in the string in u variable.
本文标签: javascriptI can39t understand this simple JS codeStack Overflow
版权声明:本文标题:javascript - I can't understand this simple JS code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745604533a2665761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论