“m.mywebsite.com”。 为此,我使用了名为 Mobile Detect 的项目 到目前为-6ren">
gpt4 book ai didi

javascript - 如何重定向 "Desktop Desktop"?

转载 作者:行者123 更新时间:2023-11-28 05:26:02 28 4
gpt4 key购买 nike

我的网站有两个版本。桌面版和移动版。

当用户通过智能手机访问我的网站时,我将其指向“移动版本”->“m.mywebsite.com”。

为此,我使用了名为 Mobile Detect 的项目

到目前为止一切顺利。

在“移动版本”中有一个按钮(“切换到桌面版本”),可以重定向到“桌面版本”。问题是,在“移动版本”中,当我单击“切换到桌面版本”按钮时,它会进入循环。

例如:

---> 单击“切换到桌面版本”按钮;
---> 页面指向“www.mywebsite.com”;
---> 页面刷新完成;
---> 由于用户使用智能手机,因此再次加载脚本并将用户重定向到“m.mywebsite.com”;

我该如何解决这个问题?

如何将用户重定向到智能手机中的桌面版本?

我的代码:

<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
<head>
<?php if( $detect->isMobile() ) : ?>
<script type="text/javascript">
window.location.href = "http://m.mywebsite.com.br";
</script>
<?php endif ?>
</head>
<body>
<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
<h1>MOBILE VERSION</h1>
<a href="http://www.mywebsite.com.br">SWITCH TO DESKTOP VERSION</a>
</div>
</body>
</html>

---------更新---------

谢谢@Uriel,你可以解决这个问题。现在页面正在按我想要的方式工作。

<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">

<?php if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false')) : ?>
<?php if( $detect->isMobile() ) : ?>

<script type="text/javascript">
window.location.href = "http://m.mywebsite.com.br";
</script>

<?php endif ?>
<?php endif; ?>

</head>
<body style="padding: 0; margin: 0">

<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
<h1>DESKTOP VERSION</h1>
</div>
</body>
</html>

最佳答案

设置链接地址

<a href="http://www.mywebsite.com.br">SWITCH TO DESKTOP VERSION</a>

发送至地址http://www.mywebsite.com.br&force_desktop=true

<小时/>

然后,在检查移动设备时,还要检查此 GET 变量是否为 false(或不存在):

if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false'))
<小时/>

这样,如果没有真正的 force_desktop 变量,并且用户使用移动设备,移动设备就会加载。

关于javascript - 如何重定向 "Desktop Desktop"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40163302/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com