Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect screen size and pixel density on the server-side?

I've been doing some research and I think I know the answer already, but I'm wondering if there's any means by which you can get a device's screen size and pixel density without the use of javascript or relying on CSS3 media queries.

Essentially, I'm looking into what it would take to get the screen resolution and pixel density so that the server could decide which image to server in a URI request.

So far I've not found anything that says this is even possible but I thought hey, why not ask?

like image 666
dougoftheabaci Avatar asked Sep 29 '11 11:09

dougoftheabaci


People also ask

Does screen size affect pixel density?

A screen's size and resolution also affect its pixel density, which means how many pixels there are per inch of screen space, and having a higher pixel density can help improve text clarity, which you can read about here.

What is screen resolution and pixel density?

In short, pixel density is the ratio between a screen's size and its resolution. For instance, the standard 1920×1080 Full HD resolution will result in a different pixel density (or pixel-per-inch ratio) on a 24″ screen (92 PPI) and on a 27″ screen (82 PPI).

How do I check my screen size in HTML?

Use window. innerWidth and window. innerHeight to get the current screen size of a page.


1 Answers

I had the same problem and solved it using a getter and a setter route for the window height. If the $height variabale is 0 the get_heigt.erb is served, otherwise the index.erb This is a one user app, so I use a global variable, with different users you would have to keep that info in cookies. Here the code that matters.

Controller:

get "/" do
  if $height == 0
    erb :get_height
  else
    erb :index
  end
end

get "/get_height" do
  erb :get_height
end

get "/set_height" do
  $height = params[:height]
  redirect "/"
end

get_height.erb

<script type="text/javascript">
  function send_message(message) {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      document.location.reload(true);
    }
    xhttp.open("GET", "http://localhost:4567/" + message + "?height=" + window.innerHeight, true);
    xhttp.send();
  }

  send_message('set_height');
</script>
like image 106
peter Avatar answered Oct 06 '22 21:10

peter