Javascript中是否有办法使浏览器识别用户的分辨率并将他重定向到我的特定页面? 像这样:
var wid=screen.width;
var hei=screen.height;
if ((wid = 1024) && (hei = 768)) {
window.redirect('page1024x768.html',"blank")
}
else {
((wid = 800) && (hei == 600)) {
window.open(page800x600.html',"_blank")
}
else {
window.open('page1366x768',"_blank")
我试过了,但它不起作用,代码有误吗?
最直接的是,你的问题是
- 您使用的是
=
(作业)而不是==
(相等性测试); - 您可能想使用
location.replace
而不是window.open
; - 你缺少一个开场白;和
- 您缺少右大括号。
这仅在页面加载时运行,那么如果有人更改其屏幕分辨率,它将不会调整;像Google这样的搜索引擎可能不喜欢重复的内容;如果有人有其他更模糊的分辨率(实际上像我一样)怎么办?一个更好的解决方案是使用Dhaivat指出的CSS媒体查询。
你可以使用Javascript来实现这一点,但是使用CSS的媒体查询来设置页面样式可能会更容易/更好。
从理论上讲,代码应该可以工作,但它有很多语法错误。
固定代码:
var wid = screen.width;
var hei = screen.height;
if (wid == 1024 && hei == 768) {
window.redirect('page1024x768.html',"blank");
}
else if (wid == 800 && hei == 600) {
window.open('page800x600.html',"_blank")
}
else {
window.open('page1366x768',"_blank")
}