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?
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.
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).
Use window. innerWidth and window. innerHeight to get the current screen size of a page.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With