简体   繁体   中英

Javascript mobile redirect script

I'm working a wordpress website that needs a mobile redirect to the mobile home page in case the user is using a cell. I'm trying to utilize this Javascript code but I'm having major difficulties getting it to work properly.

  1. I need help removing the conformation section that asks the user if they want to continue to mobile site.

  2. I also need help figuring out how to restructure the code so it doesn't keep forwarding mobile user to home page. For example, I load the page on mobile, the code runs and it forwards me to mobile page. From there I click on another link in the top navigation and it takes me back to the home page no matter what I do.

Keep in mind I am very new to this so any Input and Help from you experienced folks out there would be much appreciated.

Thank You

P.

<script type="text/javascript">
if (screen.width < 1081) {
var ref = document.referrer;
var urls = new       Array("http://www.mymainsite.com","http://m.mymobilesite.com");
var n = ref.match(urls[0]);
var m = ref.match(urls[1]);
if ((m!==null) || (n!==null)) {
stop;
}
else if (ref=='') {
var r = confirm("Small Display is Detected.\nClick \"OK\" for MOBILE  SITE.");
if (r==true) {
window.location = "http://m.mymobilesite.com";
}
else {
stop ;
}
}
else
{
window.location = "http://m.mymobilesite.com";
} 
}
</script> 

We only activate this code on your main site. Then we check whether or not your screen width is small enough and look into localStorage to see whether or not the user has made a decision before. Then we put up the confirmation box. If the user clicks okay, we go to the mobile site, if not we set the localStorage variable to true. Keep in mind that localStorage is only available IE8+

if(location.hostname === 'mymainsite'){
    if (screen.width < 1081 && !localStorage.isMainSite) {
        if(confirm('Small Display is Detected.\nClick \"OK\" for MOBILE  SITE.')){
            window.location = "http://m.mymobilesite.com";
        } else {
            localStorage.isMainSite = true;
        }
    }
}

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