在做织梦的PC端手机端一对一链接适配跳转的时候经常需要用到的就是获取当前文章链接地址、获取当前栏目链接等。一个完整的跳转地址设置方法如下:
之前做法:
内容的跳转:
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script> <script type="text/javascript">uaredirect("http://手机站网址{dede:field name='arcurl'/}");</script>
栏目列表页的跳转:
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script> <script type="text/javascript">uaredirect("http://手机站网址{dede:type}[field:typelink /]{/dede:type}");</script>
Tag页面跳转
<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script> <script type="text/javascript">uaredirect("http://m.landsj.cn{dede:tag row='1'}[field:link/]{/dede:tag}");</script>
当然主页的跳转就只需要将上述任意一处的链接中的变量去掉就行了。
现在做法将:
http://siteapp.baidu.com/static/webappservice/uaredirect.js
替换为:
function uaredirect(f) { try { if (document.getElementById("bdmark") != null) { return } var b = false; if (arguments[1]) { var e = window.location.host; var a = window.location.href; if (isSubdomain(arguments[1],e) == 1) { f = f + "/#m/" + a; b = true } else { if (isSubdomain(arguments[1],e) == 2) { f = f + "/#m/" + a; b = true } else { f = a; b = false } } } else { b = true } if (b) { var c = window.location.hash; if (!c.match("fromapp")) { if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i))) { location.replace(f) } } } } catch(d) {} } function isSubdomain(c,d) { this.getdomain = function(f) { var e = f.indexOf("://"); if (e > 0) { var h = f.substr(e + 3) } else { var h = f } var g = /^www./; if (g.test(h)) { h = h.substr(4) } return h }; if (c == d) { return 1 } else { var c = this.getdomain(c); var b = this.getdomain(d); if (c == b) { return 1 } else { c = c.replace(".","\."); var a = new RegExp("\." + c + "$"); if (b.match(a)) { return 2 } else { return 0 } } } };
原因在于:公共资源有时候会因为网络原因,严重影响网页加载时间。
主要注意的一点是:如果栏目页链接不是默认链接,且直接在更目录的需要手动设置写死对应的跳转链接。否则会出现错误。
补充:跳转适配中还可以使用配置文件【.htaccess】形式,如:
# 将 RewriteEngine 模式打开 RewriteEngine On RewriteBase / # Rewrite 规则 判断用户浏览器类型,如果是手机就跳转到手机网站 RewriteCond %{HTTP_USER_AGENT} "(baidu.Transcoder|mini|android|blackberry|googlebot-mobile|iemobile|Mobile|ipad|iphone|ipod|opera mobile|palmos|webos|ucweb|Windows Phone|Symbian|hpwOS)" [NC] RewriteRule ^(.*)$ http://m.weguiding.com/?fromPC=http://www.weguiding.com [L,R=302]
如此情况就不需要用JS代码。