位置:  首页 -> 编程语言 -> javascript -> js实现在手机中网站从pc端跳入对应移动端

js实现在手机中网站从pc端跳入对应移动端

代码如下

(function(){
	try {
        var urlhash = window.location.hash;
        var url_path = window.location.pathname;
        if (!urlhash.match("fromapp")) {//判断不是 APP
            if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iPad)/i))) {
                window.location = "移动端域名"+url_path;	      
            }
        }
    } 
		catch (err) {
    }
})();
————————————————

注释

  • window.location.hash : 返回当前 URL 的锚部分(从 # 号开始的部分)
  • window.location.pathname : 返回当前 URL 的路径部分 如 ( /index/contents/content/id/3 )
  • navigator.userAgent : 声明了浏览器用于 HTTP 请求的用户代理头的值
  • match( RegExp 对象 ) : 在字符串内检索指定的值
到底了 4

相关阅读