有没有一种简单的方法来使用JavaScript获取相对URL?我正在尝试使用
我的目标是这样的:我有两个页面;mobile.html和desktop.html。我想使用一个JavaScript文件来检测用户是在移动设备还是桌面设备上(我知道,这不是一个很好的方法..),就像这样:
window.location.href
,但它返回绝对路径。我的目标是这样的:我有两个页面;mobile.html和desktop.html。我想使用一个JavaScript文件来检测用户是在移动设备还是桌面设备上(我知道,这不是一个很好的方法..),就像这样:
var is_mobile = //alot of text from http://detectmobilebrowsers.com/
//that returns true/false, this works fine
if (is_mobile){
if (window.location.href != "mobile.html"){
window.location = "mobile.html";
}
}
else {
if (window.location.href != "desktop.html"){
window.location ="desktop.html";
}
}
绝对路径的问题在于,无论是测试 mobile.html 还是 desktop.html,它们都会进入无限循环刷新页面的状态。