[英]Javascript Screen Resolution Detection and Redirection
<script>
function detect() {
var uagent = navigator.userAgent.toLowerCase();
var mobile = false;
var search_strings = [
"iphone",
"ipod",
"ipad",
"series60",
"symbian",
"android",
"windows ce",
"windows7phone",
"w7p",
"blackberry",
"palm"
];
for (i in search_strings) {
if (uagent.search(search_strings[i]) > -1)
mobile = true;
}
return mobile;
}
if (detect())
window.location = "mydomain/mobile";
</script>
<script type="text/javascript">
if (screen.width <= 1600) {
window.location.replace('mydomain/1600');
}
if (screen.width > 1900) {
window.location.replace('mydomain/1900');
}
</script>
Hello, i have this code working fine. 您好,我的这段代码工作正常。 I want know if possible add more two screens resolutions ( 1300 and 1200 ). 我想知道是否可以添加更多两个屏幕分辨率(1300和1200)。
If access by "mobile" = mydomain/mobile 如果通过“移动”访问= mydomain /移动
If access by desktop "resolution 1900 plus" = mydomain/1900 如果通过桌面“分辨率1900加” = mydomain / 1900访问
If access by desktop "resolution 1600 till 1899" = mydomain/1600 如果通过桌面“分辨率1600至1899”访问= mydomain / 1600
If access by desktop "resolution 1300 till 1599" = mydomain/1300 如果通过桌面“分辨率1300至1599”访问= mydomain / 1300
If access by desktop "resolution 1024 till 1299" = mydomain/1200 如果通过桌面“分辨率1024至1299”访问= mydomain / 1200
Thanks!! 谢谢!!
First of all, a better approach to this is CSS rule @media . 首先,更好的方法是CSS规则@media 。
If you want to use javascript
simply add if else
statements... 如果您想使用javascript
只需添加if else
语句...
if (screen.width >= 1900) {
window.location.replace('mydomain/1900');
} else if (screen.width >= 1600 && screen.width < 1900) {
window.location.replace('mydomain/1600');
} else if (screen.width >= 1300 && screen.width < 1600) {
window.location.replace('mydomain/1300');
} else if (screen.width >= 1024 && screen.width < 1200) {
window.location.replace('mydomain/1024');
} else {
alert("error");
}
Also I would suggest reduce function detect()
and externalize constants: 我也建议减少function detect()
并外部化常量:
var uagent = navigator.userAgent.toLowerCase();
var mobile = false;
var search_strings = [
"iphone",
"ipod",
"ipad",
"series60",
"symbian",
"android",
"windows ce",
"windows7phone",
"w7p",
"blackberry",
"palm"
];
function detect() {
for (i in search_strings) {
if (uagent.search(search_strings[i]) > -1){
mobile = true;
return mobile;
}
}
return mobile;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.