[英]Redirecting a mobile user to a desktop version
I have a Wordpress website that is using device detection to redirect users browsing on their mobile device to a specific page using the following: 我有一个使用设备检测功能的Wordpress网站,它使用以下操作将在移动设备上浏览的用户重定向到特定页面:
<script>
if( /Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent) ) {
window.location = "176.32.230.17/domain.co.uk/m";
}
</script>
This code currently sits on the homepage template for domain.co.uk. 该代码当前位于domain.co.uk的主页模板上。 Just after the opening body tag.
刚打开身体标签之后。 The detection and redirection works well however, I need to provide a link back to the desktop version of the website on that mobile page.
检测和重定向效果很好,但是,我需要在该移动页面上提供一个指向网站桌面版本的链接。 So just a simple link:
所以只是一个简单的链接:
<a href="http://176.32.230.17/domain.co.uk/?ref=desktop">Link back to desktop website</a>
This works in that it directs the user back to the domain.co.uk address however, because the placement of the detection/redirection code is on the homepage template on domain.co.uk, the user gets redirected back to the mobile version. 这样做是因为它会将用户引导回domain.co.uk地址,因为检测/重定向代码的位置位于domain.co.uk的主页模板上,因此用户将被重定向回移动版本。 How can I fix this?
我怎样才能解决这个问题?
You'll need someway to convey the fact that the user has already been redirected once. 您需要某种方式来传达用户已经被重定向一次的事实。 If you're using PHP, you could have a conditional redirection using a GET variable.
如果您使用的是PHP,则可以使用GET变量进行条件重定向。 If you're just using Javascript, maybe use a cookie.
如果您仅使用Javascript,则可以使用Cookie。 HTML5 also supports some kind of data storage as well, but I'm not familiar with that.
HTML5也支持某种数据存储,但是我对此并不熟悉。
EDIT: Actually, you could just tack on a GET variable to the address and not even use PHP. 编辑:实际上,您可以将GET变量附加到地址上,甚至不使用PHP。 Just get the address using
window.location.href
and see if the variable is set using Javascript only. 只需使用
window.location.href
获取地址,然后查看是否仅使用Javascript设置了变量。
EDIT2: Too much got put in the comments, so let me put this here. EDIT2:注释中放入太多内容,所以让我将其放在此处。 The link should be changed to:
链接应更改为:
<a href="http://176.32.230.17/domain.co.uk/?ref=desktop#redirected">Link back to desktop website</a>
And the redirection code can be changed to: 重定向代码可以更改为:
<script>
if( /Android|webOS|iPhone|iPod|BlackBerry|IEMobile/i.test(navigator.userAgent) &&
!window.location.href.contains("#redirected") ) {
window.location = "176.32.230.17/domain.co.uk/m";
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.