html 桌面到移动设备重定向,反之亦然
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 桌面到移动设备重定向,反之亦然相关的知识,希望对你有一定的参考价值。
// Desktop to Mobile redirect and vise-versa
// Usage: Add right after the opening <html> tag
// For the desktop template:
<!-- Browser detect and redirect -->
<script type="text/javascript">
(function() {
// Get URL query strings
function getQS(v) {
var qs = window.location.search.substring(1);
var qsArray = qs.split("&");
for (i = 0; i < qsArray.length; i++) {
var qp = qsArray[i].split("=");
if (qp[0] == v) {
return true;
}
}
}
if (getQS('force')) {
document.getElementsByTagName('a').onclick = function(el) {
if (el.href.indexOf('?') > 0) {
el.href = el.href + '&force=1';
} else {
el.href = el.href + '?force=1';
}
}
}
if (getQS('m') === undefined) {
if (getQS('force')) return;
var curUrl = window.location.href;
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var getLastChar = curUrl.substr((curUrl.length) - 1);
if (((/Android/i).test(userAgent) && (/Mobile/i).test(userAgent)) || (/BlackBerry|iPhone|iPod|Opera Mini|IEMobile/i).test(userAgent)) {
if (curUrl.indexOf('?') > 0) {
window.location.replace(curUrl + '&m=1');
} else {
window.location.replace(curUrl + '?m=1');
}
}
}
})();
</script>
// For the mobile template:
<!-- Browser detect and redirect -->
<script type="text/javascript">
(function() {
// Get URL query strings
function getQS(v) {
var qs = window.location.search.substring(1);
var qsArray = qs.split("&");
for (i = 0; i < qsArray.length; i++) {
var qp = qsArray[i].split("=");
if (qp[0] == v) {
return true;
}
}
}
if (getQS('m')) {
if (getQS('mobile')) return;
var curUrl = window.location.href;
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
var getLastChar = curUrl.substr((curUrl.length) - 1);
var newUrl = false;
if (!(((/Android/i).test(userAgent) && (/Mobile/i).test(userAgent)) || (/BlackBerry|iPhone|iPod|Opera Mini|IEMobile/i).test(userAgent))) {
newUrl = curUrl.split('?');
newUrl = newUrl[0];
if(newUrl) {
window.location.replace(newUrl);
}
}
}
})();
</script>
以上是关于html 桌面到移动设备重定向,反之亦然的主要内容,如果未能解决你的问题,请参考以下文章