简体   繁体   English

Javascript移动重定向脚本

[英]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. 我正在工作一个wordpress网站,如果用户使用单元格,则需要将其重定向到移动主页。 I'm trying to utilize this Javascript code but I'm having major difficulties getting it to work properly. 我正在尝试利用此Javascript代码,但在使其正常工作方面遇到很大困难。

  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. 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. 然后,我们检查您的屏幕宽度是否足够小,并查看localStorage以查看用户之前是否已做出决定。 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. 如果用户单击“确定”,则转到移动站点,否则,将localStorage变量设置为true。 Keep in mind that localStorage is only available IE8+ 请记住,localStorage仅适用于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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM