有没有一种简单的方法可以使用 javascript 获取相对 url?我试图使用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/desktop.html 时,它们都会进入无限循环 pagerefresh..