您的位置:首页 > 运维架构 > 网站架构

Web_JavaScript_实现:手机访问网站,自动跳转到其网站手机版;

2014-09-10 17:22 567 查看
直接进入正题:以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转!

<script src="http://siteapp.baidu.com/static/webappservice/uaredirect.js" type="text/javascript"></script>

<script type="text/javascript">uaredirect("http://4g.xxx.com");</script>

将代码网址部分改成你网站的手机网址就Ok,当然可以是任意网址!这应该就是传说中的WAP自动跳转吧! 

->uaredirect.js代码

<span style="font-size:18px;">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
}
}
}
};</span>


 以下代码更全面些,经测试,可以兼容windows phone;

<scripttype="text/javascript"
language="javascript"src="/fkwap.js"></script>

 fkwap.js文件点击这里下载fkwap.zip;

->fkwap.js

var browser_class = navigator.userAgent;
var browser_class_name1 = browser_class.match("Mobile");
var browser_class_name2 = browser_class.match("mobile");
var location_url = window.location.href;
if (browser_class_name1 != null || browser_class_name2 != null){
if (location_url.match("wap") == null){
window.location.href="http://3g.lybase.net";
}
} else
{
if (location_url.match("3g") != null || location_url.match("wap") != null){
window.location.href="http://3g.lybase.net";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: