编程爱好者之家
今天要实现当电脑端访问手机端的链接地址,要实现自动跳转到电脑的页面地址,下面编程爱好者之家为大家详细讲解下如何用js实现。
redirectMobile = function() {
var nowUrl = location.href,
nowHost = location.host,
nowPathname = location.pathname,
jumpUrl = '',
mobileUrl = 'https://www.codelovers.cn';
mobileHurl = GetUrlRelativePath()
var sUserAgent= navigator.userAgent.toLowerCase();
var bIswindows= sUserAgent.match(/windows/i) == "windows";
if (bIswindows) { try {
if (nowUrl == 'https://m.codelovers.cn/') {
jumpUrl = mobileUrl;
}else if (nowHost != 'www.codelovers.cn') {
var match = nowPathname.match(/^\/[a-zA-Z0-9]*(\/)?(.*)?/i);
if (match != null) jumpUrl = mobileUrl + mobileHurl;
}
if (jumpUrl) location.href = jumpUrl;
} catch (e) { }
}
}
function GetUrlRelativePath()
{
var url = document.location.toString();
var arrUrl = url.split("//");
var start = arrUrl[1].indexOf("/");
var relUrl = arrUrl[1].substring(start);//stop省略,截取从start开始到结尾的所有字符
if(relUrl.indexOf("?") != -1){
relUrl = relUrl.split("?")[0];
}
return relUrl;
}
redirectMobile();