UserAgent Switcher to mobile web

柔情痞子 提交于 2019-12-11 13:53:53

问题


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"?

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>

回答1:


Add this as link:

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

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
}

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"; 
}

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.

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.



来源:https://stackoverflow.com/questions/15106159/useragent-switcher-to-mobile-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!