Vinicius
Vinicius

Reputation: 22

How to make browser recognize user's resolution?

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

Answers (3)

icktoofay
icktoofay

Reputation: 129001

Most immediately, your problems are that

  1. you're using = (assignment) rather than == (equality testing);
  2. you probably want to use location.replace rather than window.open;
  3. you're missing an opening quote; and
  4. you're missing a closing brace.

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

skimberk1
skimberk1

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

Dhaivat Pandya
Dhaivat Pandya

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

Related Questions