k13*_*13i 1 javascript regex safari detection
我找到了以下代码段,该代码段检查是否使用了Safari浏览器:
var isSafari = /constructor/i.test(window.HTMLElement);
Run Code Online (Sandbox Code Playgroud)
但我真的不知道这是怎么回事。有人可以解释一下它是如何工作的吗?我知道constructor该函数返回创建RegExp对象原型并i执行不区分大小写的匹配的函数,但是究竟是HTMLElement什么呢?
在safari中,window.HTMLElement返回一个名为的函数HTMLElementConstructor。
因此,让我们这样做:
/constructor/i.test(function HTMLElementConstructor() {}) // return true
Run Code Online (Sandbox Code Playgroud)
但是使用其他浏览器(FF,Chrome)时,它会返回 HTMLElement
/constructor/i.test(function HTMLElement() {}) // return false
Run Code Online (Sandbox Code Playgroud)
但是感谢您的观察!我希望我们可以使用它以及这里提到的方法:检测Safari浏览器
| 归档时间: |
|
| 查看次数: |
5706 次 |
| 最近记录: |