Javascript document.location重定向到错误的url

Javascript document.location重定向到错误的url,javascript,redirect,Javascript,Redirect,我使用下面的代码重定向是否在移动设备上访问了该站点 <script type="text/javascript"> <!-- if (screen.width <= 978) { document.location = "mobile.mysite.com"; } //--> </script> 我在很多设备上测试了它。为什么网站只重定向到www.mysite.com/mobile.mysite.com,而不是mobile.mysite.com?尝

我使用下面的代码重定向是否在移动设备上访问了该站点

<script type="text/javascript">
<!--
if (screen.width <= 978) {
document.location = "mobile.mysite.com";
}
//-->
</script>


我在很多设备上测试了它。为什么网站只重定向到
www.mysite.com/mobile.mysite.com
,而不是
mobile.mysite.com

尝试使用
window.location.href
而不是
document.location
:)

试试这个

<script type="text/javascript">
<!--
if (screen.width <= 978) {
document.location.href = "http://mobile.mysite.com";
}
//-->
</script>

您需要在开头指定
'http://'
或只指定
'/'
,否则URL将被视为相对URL而不是绝对URL

if (screen.width <= 978) {
    document.location.href = "//mobile.mysite.com";
}

if(screen.width)您是否尝试使用绝对URL路径而不是相对URL路径?