简体   繁体   中英

View mobile site link in full site

While I have a successful method of getting users from our mobile site to our full site, I need help getting them from the full site back to the mobile site.

I'm trying to use a combination of CSS and Javscript-written HTML (below) which is using server side detection. Basically the detection determines if the user is on a smart phone and, if so, writes the banner HTML. It should look like this , but unfortunately this isn't working. PHP isn't an option.

My question is, how can I get the "View Mobile" banner to appear only on smart phones (ie iPhone & Android)?

Here's my page and code:

<meta name="viewport" content="width=device-width, user-scalable=yes" /> 
<link rel="stylesheet" type="text/css" href="css/mobile.css" media="only screen and (max-width: 640px)" />

...

<body> 
<script type="text/javascript">
<!--
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
device_class = readCookie('COOKIE_DEVICE_CLASS');
if (device_class == 'smart') {
document.write('<div id="view-mobile"><a href="http://m.stanford.edu/">View Stanford Mobile</a></div>');
}
// -->
</script>

Your cookie is not being set. I visited both stanford.edu and m.stanford.edu, and I do not get a COOKIE_DEVICE_CLASS cookie. If the cookie is created and contains 'smart' your code will work.

This might work:

var userAgent= navigator.userAgent.toLowerCase();

if (!!/iphone/.test(userAgent) || !!/android/.test(userAgent)) {
    // write banner
}

Create htaccess on your mobile site then add this code.

RewriteCond %{HTTP_COOKIE} !FULLSITE=yes
### Your current conditions...
RewriteRule (.*) http://your URL_mobile_add? [R,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM