Load iframe content with different user agent(使用不同的用户代理加载 iframe 内容)
问题描述
是否可以使用不同的用户代理加载 iframe ?为 iframe 使用移动用户代理将有助于我的应用将移动网站显示为悬停弹出窗口.
Is it possible to load an iframe with different user agent ?? Using a mobile user agent for an iframe will help my app to show mobile sites as hover popup.
例如,只有当用户代理来自移动端时,Google 才会显示移动搜索结果页面.
For example, Google shows the mobile search results page only if the user agent is from a mobile one.
是否有任何替代解决方案,或者这个想法是否涉及任何安全风险??
Is there any alternate solutions or is there any security risks involved in this idea ??
推荐答案
首先,你必须创建一个函数来改变用户代理字符串:
First of all, you must create a function to change the user agent string:
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
});
}
}
}
那么你需要定位iframe元素:
Then you need to target the iframe element:
setUserAgent(
document.querySelector('iframe').contentWindow,
'Aakash Chakravarthy Mobile Agent'
);
您还可以为 iframe 设置一个 ID,并以该 ID 而非页面上的所有 iframe 元素为目标.
You may also set an ID to the iframe and target the ID instead of all iframe elements on the page.
这篇关于使用不同的用户代理加载 iframe 内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用不同的用户代理加载 iframe 内容


基础教程推荐
- 如何在特定日期之前获取消息? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01