Reputation: 22
Is there a way in Javascript to make the browser recognize the user's resolution and redirect him to my specific page? something like that:
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")
I tried but it's not working, is the code wrong?
Upvotes: 0
Views: 69
Reputation: 129001
Most immediately, your problems are that
=
(assignment) rather than ==
(equality testing);location.replace
rather than window.open
;However, keep in mind that if this only runs on page load, then if someone changes their screen resolution, it won't adjust; search engines like Google might not like duplication of content; and what if someone has some other, more obscure resolution (like me, in fact)? A much better solution would be to use, say, CSS media queries as pointed out by Dhaivat.
Upvotes: 3
Reputation: 2094
Theoritically that code should work, but it's got quite a few syntax errors.
Fixed code:
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")
}
Upvotes: 1
Reputation: 6536
You could use Javascript for this, but it will probably be much easier/better to style your pages using CSS's media queries.
Upvotes: 1