问题描述
确定这是一个边缘的情况下我也设法在降落。我测试我对新的Tizen OS的应用程序。我的JS code有成千上万导航仪检查。是这样的:
navigator.userAgent.toLocaleLowerCase()的indexOf(机器人)|| navigator.userAgent.toLocaleLowerCase()的indexOf(iPad是)
现在Tizen OS的我的测试设备上的userAgent没有两者的字符串。我的很多CSS和JS的是打破结果。我在POC模式,现在,不想花时间在增加一个额外的检查,所有这些条件。有没有一种方法以编程方式设置的userAgent?沿着线的东西:
navigator.userAgent的+ =Tizen//不起作用。
说,它的读写性能。我不能够,虽然修改。帮助我。无论是另一种聪明的办法来欺骗的userAgent或正确的方法来设置此。谢谢你。
In that case it is your JS code that is at fault.
You've basically just discovered the ultimate reason why doing UA string detection is considered really bad practice.
In short, if your code looks as described, then you're doing something wrong. There are very few legitimate reasons for detecting the UA string, and none of those reasons apply to code running on the client itself.
And thousands of lines of this stuff in the browser???? That's can only be doing bad things to the performance of your site.
The userAgent string is a read-only value.
However, you can override the getter, so there are ways of making it writable. Ugly, but possible.
I'd really recommend avoiding doing this though.
Even without browser vendors deliberately changing their UA strings to break this kind of code (which they do), your task is fundamentally never-ending; you're going to have to keep revisiting this code every time a new device/browser/version is released, and your thousands of lines of code just will keep getting bigger and bigger.
Plus of course, it will be completely unreliable if a user modifies his browser's UA string.
Fix it now with a hack if you must, but be aware that this will keep eating more and more of your time. You really need to consider switching to a better coding practice such as feature detection instead of UA detection.
这篇关于如何设置导航器的userAgent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!