Casualty
Casualty

Reputation: 43

Trying to display monitor aspect ratios via html

<SCRIPT language="JavaScript">

    height = screen.height;
    width = screen.width;
    aspect = width/height;

    if (width==1920)(height==1200)
    {
       x="16:10";
    }
    else if (width==1680)(height==1200)
    {
       x="16:9";
    }
    else
    {
       x="unknown";
    }

    document.write( x );

</SCRIPT>

This is my exact code. I just started javascript this morning. It's currently displaying nothing.

Upvotes: 0

Views: 72

Answers (4)

Roko C. Buljan
Roko C. Buljan

Reputation: 206506

LIVE DEMO

function ar() {
  // Greatest Common Divisor algorithm loop:
  function gcd(x,y){return !y?x:gcd(y,x%y);} 
  var s=screen, w=s.width, h=s.height, r=gcd(w,h);
  return w/r+':'+h/r; // Return the Aspect-Ratio String
}

document.body.innerHTML = ar();

https://en.wikipedia.org/wiki/Euclidean_algorithm

Upvotes: 1

Gabin
Gabin

Reputation: 1318

There is a couple of things that is incorrect in your code.

  1. HTML tags must be written in lowercase
  2. It's not language="JavaScript" but type="text/javascript" (you can avoid this declaration if you're building an HTML5 website)
  3. As others said, if you want two conditions to be true at the same time you have to write width == 1920 && height = 1200
  4. When declaring a variable, use "var" before the name var height = screen.height; You just need it when creating the variable, to use it just call height. Not using "var" will create a global variable but that is for another lesson, just remember it's better to do it this way.

If you're using Chrome, right click on the document and then "inspect". The inspector will pop out. This tool is really powerfull and may seems complex but just read what is written in the "console" tab. You'll eventually know there is an error about your syntax at line x, etc. (The console is available in nearly all browsers but I don't really know how they works on others than Chrome).

Upvotes: 0

Aren
Aren

Reputation: 55966

Firstly you're using If wrong:

if (width==1920)(height==1200)

Is invalid syntax, if you want to and two or more conditions together use the && operator:

if (width==1920 && height==1200)

Secondly this example will only "work" for two known resolutions, I suggest you check the aspect variable instead of the height and width. Here's an expandable example:

var ratios = [
   {name: "16x10", a: 16/10},
   {name: "16x9", a: 16/9},
   {name: "4x3", a: 4/3}
   // etc...
]

var aspect = screen.width/screen.height;
var aspectName = "Unknown";

for(var i = 0; i < ratios.length; i++)
{
  var ratio = ratios[i];
  if (aspect == ratio.a) { 
    aspectName = ratio.name;
    break;
  }
}

document.write(aspectName);

Upvotes: 1

Boann
Boann

Reputation: 50041

This is not a correct syntax:

if (width==1920)(height==1200)

Use the && (and) operator to tell if both conditions are true:

if (width==1920 && height==1200)

P.S. For a more general solution to generate the x string, treat width and height as numerator and denominator of a fraction and simplify the fraction by dividing by common prime factors.

Upvotes: 1

Related Questions