我有一段javascript:
<script>
var reg = "website.com";
var mob = "website.com/m";
if(window.innerWidth >= 1024) {
if(window.location.href != reg)
window.location.replace("http://" + reg + window.location.pathname + window.location.search)
}
else {
if(window.location.href != mob)
window.location.replace("http://" + mob + window.location.pathname + window.location.search)
}
</script>
当我运行它时,它一直在重新加载页面--你知道如何解决这个问题吗?
发布于 2014-11-14 14:21:35
您正在将window.location.hostname
与mob
进行比较。您是否尝试过记录这两个日志,以查看当宽度小于1024时会发生什么?
console.log(window.location.hostname); // -> website.com
console.log(mob); // -> website.com/m/
如你所见,window.location.hostname
is the hostname,即斜杠前的位。
尝试基于window.location.href
(或window.location.pathname
)进行比较。
发布于 2014-11-14 15:13:11
http://以及不以http://.开头的"reg“和"mob”返回window.location.href请在将http添加到变量后重试。
https://stackoverflow.com/questions/26932032
复制