1

I am using the following script to redirect a mobile user to my mobile site.

I have my code in header.php which i then include via my index.php file.

header.php:

<script>
function urlParam(name){
            var results = new RegExp('[\\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);
            if(results)
                return results[1] || 0;
            else
                return '';
        }

        if(urlParam('view') == 'full'){ 
        }
        if(urlParam('view') == ''){
            // <![CDATA[
            var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
            if (mobile) { 
                    document.location = "http://m.hewdenportal.co.uk";  
            }  
            // ]]>
        }
</script>

index.php:

include 'header.php';

on my mobile site i have the following link which should take the user to my full site:

<a href="http://www.hewdenportal.co.uk?view=full" data-ajax="false">Full Site</a>

for some reason this doesn't seem to keep the user on the full site and keeps redirecting them to the mobile site. please can someone show me where i am going wrong?

Thanks

James Gayle
  • 157
  • 1
  • 1
  • 12

1 Answers1

2

Better would be

function getUrlParameter(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
}         

var viewType = getUrlParameter('view');
var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  


if(viewType=='full'){
  //Redirect to Full site
  document.location = "http://www.hewdenportal.co.uk";  
} else if(viewType=='mobile'){
  //Redirect to Mobile 
  document.location = "http://m.hewdenportal.co.uk";   
} else if(mobile){
    document.location = "http://m.hewdenportal.co.uk";  
} else {
    //No parameter , show desktop site
    document.location = "http://www.hewdenportal.co.uk";  
}

Explanation : First test if he clicked on Mobile link or Desktop , if clicked on desktop , then show full site , if clicked on mobile link show mobile .And if he is on mobile and didnt click any link then by default we check for his device and show him mobile site.

For more info Read here

Community
  • 1
  • 1
Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73
  • thanks for this however what if the user clicks to go to the full site but leaves the page and comes back later on? they will always go back to the full site, but is there a way to take them back to the mobile site by default? until at least they click the go to full site button on the mobile site? thanks – James Gayle Mar 06 '15 at 16:20
  • Comes back later means ? You mean closing tab and after an hour opening new tab ? OR you mean just press back button after an hour ? – Pratik Joshi Mar 06 '15 at 16:24
  • well let's say come back later would be if the user closes the browser and then comes back after an hour or the next day having previously clicked to go to the full site, then i would want them to automatically be redirected back to the mobile site again on the next time they opened the site. and only take them back to the full site on clicking the full site link. thanks – James Gayle Mar 06 '15 at 16:30
  • @JamesGayle , buddy you `Can't` do all things from Client side coding like jQuery , javascript . You will need to maintain cookies regarding to specific device/machine and all info about browsing etc ,and accordingly you will do operation in Server side like PHP. – Pratik Joshi Mar 06 '15 at 16:32