问题
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