3 回答
TA贡献1783条经验 获得超4个赞
尝试:
navigator.__defineGetter__('userAgent', function(){
return 'foo' // customized user agent
});
navigator.userAgent; // 'foo'
在FF2和FF3中进行了尝试。
TA贡献1821条经验 获得超4个赞
以下解决方案可在Chrome,Firefox,Safari,IE9 +和iframe中使用:
function setUserAgent(window, userAgent) {
if (window.navigator.userAgent != userAgent) {
var userAgentProp = { get: function () { return userAgent; } };
try {
Object.defineProperty(window.navigator, 'userAgent', userAgentProp);
} catch (e) {
window.navigator = Object.create(navigator, {
userAgent: userAgentProp
});
}
}
}
例子:
setUserAgent(window, 'new user agent');
setUserAgent(document.querySelector('iframe').contentWindow, 'new user agent');
添加回答
举报