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 对象 ) : 在字符串内检索指定的值

猜你喜欢

转载自blog.csdn.net/qq_42961790/article/details/106047473
今日推荐