所以我这里有一小段代码,用于检测是移动浏览器还是桌面浏览器。这个功能是有效的,但是我尝试根据浏览器提供不同的iframe,但是iframe没有加载。请帮忙修复!
<html>
<body>
<script>
/* 将用户设备信息存储在一个变量中 */
let details = navigator.userAgent;
/* 创建一个包含一些移动设备关键词的正则表达式,
用于在details字符串中搜索 */
let regexp = /android|iphone|kindle|ipad/i;
/* 使用test()方法在details中搜索regexp,
它返回一个布尔值 */
let isMobileDevice = regexp.test(details);
if (isMobileDevice) {
document.write("您正在使用移动设备");
} else {
<iframe target="_parent" src="https://google.com/" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;"></iframe>
}
</script>
</body>
</html>
我尝试了一个PHP版本,但没有成功。请帮忙!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
如果设备检测到用户代理是移动设备,则会显示不兼容的设备消息,否则将继续浏览器活动,即使他们在移动浏览器中启用了桌面模式,仍然会显示不兼容的消息
<html> <head> <style type="text/css"> body { margin: 0 auto; padding: 0; } #root { display: grid; justify-content: center; align-items: center; width: 100vw; height: 100vh; } #browser { border: 0; width: inherit; height: inherit; } </style> </head> <body> <div id="root"></div> <script type="text/javascript"> let device = navigator.userAgent; let isMobile = device.includes("Android") || device.includes("iPhone") || device.includes("iPad"); let root = document.querySelector("#root"); let message = () => { let p = document.createElement("p"); p.textContent = "您正在使用移动设备。"; root.appendChild(p); }; let browser = () => { let browser = document.createElement("iframe"); browser.id = "browser"; browser.target = "_parent"; browser.src = "https://bing.com"; browser.crossOrigin = "anonymous"; root.appendChild(browser); } if (isMobile) { message(); } else { browser(); } </script> </body> </html>而且你可以使用三元运算符代替IF ELSE语句。