织梦dedecms手机userAgent跳转JS代码

在新织梦的default模板中,除了原有的模板外,多了些手机模板,下面讲讲织梦dedecms手机userAgent跳转JS代码,通过userAgent来自动识别终端然后自动跳转,从而实现pc的手机端适配,主要手机模板如下:

index_m.htm 首页模板

inde_default_m.htm 频道页模板
list_default_m.htm 列表页模板
list_default_sg_m.htm 列表页模板
article_article_m.htm 内容页模板
article_default_m.htm 内容页默认模板
search_m.htm 搜索页模板
head_m.htm 顶部模板
footer_m.htm 底部模板
* 首页模板中添加如下代码:

<meta http-equiv="mobile-agent" content="format=xhtml;url={dede:global.cfg_mobileurl/}/index.php"/>

<script type="text/javascript">if(window.location.toString().indexOf('pref=padindex') != -1){}else

{if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) ||

(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-

|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){if(window.location.href.indexOf("?

mobile")&lt;0){try{if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test

(navigator.userAgent)){window.location.href="{dede:global.cfg_mobileurl/}/index.php";}else if

(/iPad/i.test(navigator.userAgent)){}else{}}catch(e){}}}}</script>

* 列表页模板添加如下代码:

 

<meta http-equiv="mobile-agent" content="format=xhtml;url={dede:global.cfg_mobileurl/}/
list.php?tid={dede:field.id/}"/>

<script type="text/javascript">if(window.location.toString().indexOf('pref=padindex') != -1){}else

{if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) ||

(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-

|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){if(window.location.href.indexOf("?

mobile")&lt;0){try{if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test

(navigator.userAgent)){window.location.href="{dede:global.cfg_mobileurl/}/list.php?tid=

{dede:field.id/}";}else if(/iPad/i.test(navigator.userAgent)){}else{}}catch(e){}}}}</script>

*内容页模板添加如下代码

 

<meta http-equiv="mobile-agent" content="format=xhtml;url={dede:global.cfg_mobileurl/}/
view.php?aid={dede:field.id/}"/>

<script type="text/javascript">if(window.location.toString().indexOf('pref=padindex') != -1){}else

{if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) ||

(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|
MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){if(window.location.href.indexOf("?

mobile")&lt;0){try{if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test

(navigator.userAgent)){window.location.href="{dede:global.cfg_mobileurl/}/view.php?aid=

{dede:field.id/}";}else if(/iPad/i.test(navigator.userAgent)){}else{}}catch(e){}}}}</script>

其中上面的js是电脑网站跳转到手机网站的代码,而是用来告诉百度,手机网站的地址,主要用于seo。

pc模板添加上面代码后,手机访问网站时,就会自动跳转到手机网站模板了。

未经允许不得转载:跨屏互联 » 织梦dedecms手机userAgent跳转JS代码

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏