JS implementation of PC version of the Web page automatically identify mobile clients and jump

Source: Internet
Author: User


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&GT;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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.