0

I'm using code to redirect mobile devices that are under 699 pixels wide to go to our mobile site. This method utilizes JavaScript and cookies and follows some basic logic:

  • Do not run logic for redirect if cookies are disabled
  • Do not redirect if cookie skipmobile is set to 1
  • Redirect only if skipmobile is not 1, and your mobile device is listed below and under 699 pixels wide.

    //{{Full Site Code}} Only run logic if cookies are enabled.
    if(navigator.cookieEnabled){
        //If the cookie skipmobile is already set do not redirect to mobile.
        if (document.location.search.indexOf("skipmobile") >= 0) {
            document.cookie = "skipmobile=1";
        }
        //If the device is one of the types listed below and is under 699 pixels wide, redirect to the mobile site.
        else if (((/Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) && screen.width < 699)
                && document.cookie.indexOf("skipmobile") == -1)
            {
                document.location = "'.MOBILE_SITE.$direct.'";
            }
        }
    

On the mobile site I simply have a link like the following that someone can click to set the cookie

http://www.example.com?skipmobile=1

This code works properly for me and most people, but we are having customers saying When they click the full site link it sends them right back to the mobile site. According to the code this means they do have cookies enabled but their cookie isn't getting set.

Is there something I need to do to this code that's missing?

UPDATE: So this problem is a bit of an oddball. One of our employees is having the issue as well so we at least have a phone to test on. We have a live site and a dev site. It works for him of we go to the dev site and redirect but it doesn't for the live...

Does this help anyone come up with conclusions? The code is the same on both sites.

Dom
  • 858
  • 2
  • 11
  • 30
  • Have you ever been able to reproduce the problem _yourself_, or seen it reproduced? Also, do you know what devices are having the problem? – Annonymous Apr 06 '15 at 13:22
  • Just updated the question. One of our employees does have the problem on his phone. – Dom Apr 06 '15 at 13:39
  • What browser is he using, and on which OS, may I ask? – Annonymous Apr 06 '15 at 13:40
  • He has an iphone 5 fully updated and is using safari. His chrome did work for both live/dev His safari worked only for dev – Dom Apr 06 '15 at 13:44
  • He just cleared his cache/cookies/history again and it worked now apparently.. Is there something I can put in the code that can clear the cookie without or something cache for that page? For that initial redirect – Dom Apr 06 '15 at 13:46

1 Answers1

1

You should try deleting all cookies related to your site beforehand, as this should clear up any problems. This is a link to a great function that should do this for you:

Clearing all cookies with JavaScript

You could also put that you don't want it to cache for a while in the headers (using the cache control var) to make sure that if the phone is storing any problems these are removed

Community
  • 1
  • 1
Annonymous
  • 968
  • 1
  • 9
  • 22