UserAgent切换器到移动网络 [英] UserAgent Switcher to mobile web

查看:94
本文介绍了UserAgent切换器到移动网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript作为用户代理将主网站重定向到移动网站.但我无法在移动设备上切换到桌面视图.

I am using javascript as useragent to redirect main website to mobile website. but i can not switch to desktop view in mobile device.

是否可以通过完整网站"链接重定向到移动设备上的主要网站?

Any ways to redirect to the main website on mobile device by link "Full Website"?

这是我正在使用的javascript:

This is javascript i am using:

<script type="text/javascript">// <![CDATA[ 
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windowssce|palm/i.test(navigator.userAgent.toLowerCase())); 
if (mobile) { 
document.location = "/mobile"; 
} 
// ]]>
</script>

推荐答案

将此添加为链接:

<a href="#" onclick="goToDesktopVersion()">Desktop version</a>

和javascript(您需要实现注释中提到的代码):

And the javascript (you need to implement the code mentioned as comments):

function goToDesktopVersion(){
    // 1.) set a cookie to remember you want the deskop version
    // 2.) set window.location to your desktop version
}

并在您的检测代码(实现注释代码)中考虑cookie:

And consider the cookie in your detection code (implement commented code):

function keepDeskopVersionCookieIsSet(){
    // find out if the cookie is set and return true or false
}

...

var mobile = ... 
if (mobile && !keepDeskopVersionCookieIsSet() ) { 
    document.location = "/mobile"; 
}

需要Cookie,以便在单击桌面版本"链接后,移动客户端不会再次重定向到移动版本.

The cookie is needed in order that the mobile client will not get redirected again to the mobile version after the "Desktop version" link was clicked.

cookie是一个小数据,它存储在客户端的浏览器中,以保留一些信息.在这种情况下,这是用户想要保留页面的桌面版本的信息. Cookies总是在服务器和客户端之间相互发送,因此您可以在客户端(浏览器)或服务器上进行设置.在浏览器中,您可以使用Javascript设置Cookie.我建议不要使用一些现有的帮助程序代码,而不是从头开始编写保存cookie的所有代码. 为您工作.

A cookie is a small piece of data which is stored on the client's browser to keep some information. In this case, this is the information that the user wants to keep the desktop version of your page. Cookies are always sent between server and client to each other, so you can the set on the client (browser) or on the server as well. In a browser, you can set a cookie using Javascript. Instead of writing all the code from scratch which is needed to save the cookie, I would recommend to use some existing helper code which does the work for you.

这篇关于UserAgent切换器到移动网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆