I cannot understand this simple JS code
I cannot understand this code. If it is RegExp, can it be easier to do? Or is it already widely compatible? (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)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
}
It's simple:
String.indexOf("webkit")
a source to share
It first looks for "webkit" (ignoring case) in the string u
, trying to determine that the browser is Safari.
If it doesn't find it, it looks for "mozilla" (without "compati") or "opera" in an attempt to determine if the browser is Firefox or Opera. Again, searches ignore case ( /i
).
EDIT
Code /.../i.test()
is a regular expression that is embedded in JavaScript.
a source to share
test () in javascript is a regular expression test function. You can read about it here.
This method checks if the regular expression matches the string, returning true if successful and false if not. The test method can be used with a string literal or a string variable.
the code:
rexp = /er/
if(rexp.test("the fisherman"))
document.write("It true, I tell you.")
Output:
It true, I tell you.
Also here is another great page that goes into more detail on this feature.
Searches for a match between the regular expression and the specified string. Returns true or false.
a source to share
This is similar, but returns the 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;
})();
alerts (navigator.sayswho)
a source to share
You seem to sound like some kind of browser scent. The value u
is associated with the user agent ID . And tested it with regex (build with REGExp limit syntax/
expr
/
).
a source to share