PC version of the Web page automatically identify mobile clients and jump, using JS to determine whether the mobile phone client to jump the best, especially static Web pages, the process through the JS way of User Client execution, can greatly reduce the server side of the additional processing pressure. But sometimes it needs to be judged on the server side except.
Take the Tencent network to fit the JS function as an example:
<script>
(function () {
/*
Tencent Network fit to jump
*/
var rewriteurl = function (URL) {
if (URL) {
if (Url.indexof ("/a/") >0) {
var splits = Url.split ("/"), Sitename=splits[2].split ("qq.com") [0].split ("."). Length==3?sitename=splits[2].split ("qq.com") [0].split (".") [0]+ "_" +splits[2].split ("qq.com") [0].split (".") [1]:sitename=splits[2].split ("qq.com") [0].split (".") [0],aids=url.split ("/a/") [1].split (". htm") [0].replace (/[^0-9]/g, ""), site= "";
if (typeof sitename!== "undefined" && typeof aids!== "undefined") {
if (Sitename.split ("."). LENGTH>2) {
var len = Sitename.split ("."). Length
for (Var i=0;i<len;i++) {
Site+=sitename.split (".") [i];
if (i<len-2) {
site+= "_";
}
}
}else{
Site=sitename.split (".") [0];
}
Return "http://xw.qq.com/" +sitename+ "/" +aids;
}
}
}
};
var gotomobile = function () {
var Aarry = document.getElementsByTagName ("a"), tmparray=[];
for (Var i=0;i<aarry.length;i++) {
Tmparray[i]=aarry[i].href.tostring ();
if (Tmparray[i].split ("/") [3]== "a") {
Aarry[i].href=rewriteurl (Tmparray[i]);
}
}
}
var mobilesietlist={news: "News", Photo: "Photo", Video: "Video", View: "Huati", Finance: "Finance", ENT: "ENT", Sports: " Sports ", Digi:" Digi ", Mil:" Mil ", Lady:" Lady ", Auto:" Auto ", Games:" Games ", House:" House ", Astro:" Astro ", Cul:" Cul ", Fashion: "Fashion", "2014": "Shijiebei"};
if (/android|webos|iphone| Windows phone|ipod| blackberry| Symbianos/i.test (navigator.useragent)) {
/* Bottom Page Jump * *
var url=window.location.href;
var pathname=window.location.pathname;
if (Url.indexof ("/a/") >0 && window.location.hostname!= "www.nanjing2014.org") {
if (Url.indexof ("? PC") <0) {
try{
Window.location.href=rewriteurl (URL);
}catch (e) {}
}
}else{//Channel home jump
if (Url.indexof ("? mobile") <0) {
var mobileurl=window.location.hostname.split (".") [0];
if (/news|pp|video|view|finance|ent|sports|digi|mil|fashion|auto|games|house|astro|cul|2014/i.test (MOBILEURL)) {
try{
window.location.href= ' http://xw.qq.com/m/' +mobilesietlist[mobileurl]+ '/index.htm ';
}catch (e) {
}
}
}
}
try{
Document.addeventlistener (' domcontentloaded ', Gotomobile, false);
}catch (e) {
}
}
})();
</script>
The above function is taken from the Tencent network JS adapter jump, you can refer to its logic according to the situation of their own website to achieve this process.