javascripthttp-redirectmobile

How to use Javascript to link from mobile site to full site without getting redirected back to mobile


I am calling this script from all the pages of my full site to redirect mobile users to a mobile site URL. The problem comes in when someone clicks a link (from the mobile site) to view the full site, the redirect puts the user right back to the mobile site.

<script type="text/javascript">
<!--
if (screen.width <= 700) {
window.location = "http://example.com";
}
//-->
</script>

Is there something I can add to this JS code that will not allow the redirect to occur if the user is coming from the mobile site URL? I only want to use JavaScript in my case, so if anyone has a JS solution; that would be what I am looking for.


Solution

  • This in one way, it will remember the user choice 100 days. http://www.w3schools.com/js/js_cookies.asp

    Use the functions from the link i provided: and then change this if;

    <script type="text/javascript">
    <!--
    if (screen.width <= 700 && getCookie("mobileRedirected")!="true"){
    window.location = "http://domain.com";
    }
    //-->
    </script>
    

    On the link back to that page do:

    <a href="#" onclick="setCookie('mobileRedirected','true',100);document.location='index.html';">LINK</a>
    

    Next time user gets to index it will know it has clicked his way away from mobile.html

    That is if you wish the user to be able to choose between sites, if not (only show mobile once) you could do this:

       <script type="text/javascript">
        <!--
        if (screen.width <= 700 && getCookie("mobileRedirected")!="true"){
    setCookie('mobileRedirected','true',100);
        window.location = "http://domain.com";
        }
        //-->
        </script>
    

    And skip the link code all together

    Full working example of code above

       <script type="text/javascript">
       function setCookie(c_name,value,exdays){
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;
    }
    function getCookie(c_name){
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i<ARRcookies.length;i++){
      x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
      y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
      x=x.replace(/^\s+|\s+$/g,"");
      if (x==c_name)    {
        return unescape(y);
        }
      }
    }
        if (screen.width <= 700 && getCookie("mobileRedirected")!="true"){
            setCookie('mobileRedirected','true',100);
            window.location = "http://google.com";
        }
        </script>
    

    You may also window.innerWidth if you wish small browserwindows to be redirected as well. Working example link: http://allanthya.net/cookietest.php