Nathan Pizzo
Nathan Pizzo

Reputation: 1

How do I redirect to mobile, but let the user switch from then on?

I am running into a problem with re-directing to my mobile version. I do not have the programming skills to accomplish this. I am stuck.

Here is the code I have below, but first here is the logic I WANT to accomplish:

  1. When a mobile user visits a mobile version is served by default.
  2. If they click the 'View Full Site' then it switches and stays on the full site UNTIL and only IF they click the View Mobile Version (footer) and then it stays on the MOBILE version, unless they click on 'View Full Site' again.

That is it but I cannot get it to work.

Here is the code that I placed in the header:

<script type="text/javascript">
<!--
var fullSiteCookie = readCookie('fullsite');
if ( fullSiteCookie !='t' ) {
  if (screen.width <= 600) {
    window.location = "/m/mobile.html";
  }
  if ( (navigator.userAgent.indexOf('Android') != -1) ) {
    document.location = "/m/mobile.html";
  } 
  if ( (navigator.userAgent.indexOf('iphone') != -1) ) {
    document.location = "/m/mobile.html";
  }
else
  document.location="/";
}
//-->
</script>

Then the hyperlink on the full site is:

<a href="/m/mobile.html" onclick="writeCookie('fullsite', 'm')">View Mobile Site</span></a>

Likewise the mobile site has it's own link:

<a href="../index.php" onClick="writeCookie('fullsite', 't')">FULL SITE VERSION</a>

PROBLEM: I can never get the site to go (the first time) over to the mobile version. It only goes to the full desktop version. That is a show-stopper. It MUST be able to automatically send visitors to the mobile site if they are visiting the site on a mobile device. The should not have to click anything.

Any ideas? I am just not going to be able to figure this out, without help. Of course this is something I need yesterday.

Upvotes: 0

Views: 86

Answers (2)

Gary
Gary

Reputation: 13912

Similar to @Loyalty Tech's suggestion, but I have some to add.

var fullSiteCookie = readCookie('fullsite');
  /* Verify that "fullSiteCookie" is a valid value.
     Are you sure the cookie is set the first time?
  */
if ( fullSiteCookie !='t' && 
   (screen.width <= 600 ||
   navigator.userAgent.indexOf('Android') != -1 ||
/* Fix case of "iphone" to "iPhone" */
   navigator.userAgent.indexOf('iPhone') != -1)){
    window.location = "/m/mobile.html";
} else {
  window.location = "/";
}

Upvotes: 1

Stefan Ticu
Stefan Ticu

Reputation: 2103

Your elseis placed IN the if so it triggers only on the NOT iPhone condition. Try this:

if ( fullSiteCookie !='t' ) {
    if (fullSiteCookie =='m' || screen.width <= 600 || navigator.userAgent.indexOf('Android') != -1 || navigator.userAgent.indexOf('iphone') != -1) {
        window.location = "/m/mobile.html";
    } else {
        // not mobile or forced mobile
        document.location = "/";
    }

} else {
    // forced Normal 
    document.location = "/";
}

Upvotes: 1

Related Questions