简体   繁体   English

Javascript 移动重定向,页面不断刷新

[英]Javascript mobile redirect, page keeps refreshing

I'm trying to redirect all my site mobile traffic to one page.我正在尝试将我所有的网站移动流量重定向到一个页面。

so I added redirect code in header.所以我在标题中添加了重定向代码。

<script type="text/javascript">

<!-- if (screen.width <= 699)

{ document.location = "/page/mobile/";

}

//-->

</script>

The problem is that after redirecting script continuous executing because header is the same on the whole site.问题是在重定向脚本后继续执行,因为整个站点上的标头是相同的。 so it's refreshing the page over and over again.所以它一遍又一遍地刷新页面。

I want javascript to check for the /page/mobile/ in url and if it's there, do not execute redirect.我想让 javascript 检查 url 中的 /page/mobile/ ,如果它在那里,不要执行重定向。

How can I achieve this?我怎样才能做到这一点? Thanks!谢谢!

check window location in your if condition检查您的 if 条件中的窗口位置

if (window.location.href.indexOf("/mobile/")>-1){
  console.log("You are on mobile page")
}

Do this.做这个。

if ( window.location.pathname !== "/page/mobile"){
   window.location.href = "https://[your-host-name]/page/mobile";
}

So you can use window.location.pathname to access your path after host (and port)所以你可以使用window.location.pathname在主机(和端口)之后访问你的路径

https://developer.mozilla.org/en-US/docs/Web/API/Window/location https://developer.mozilla.org/en-US/docs/Web/API/Window/location

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

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