I've found that using the Modernizr.mq() function to check media queries is a much more accurate way to run Javascript based on the viewport width, because it matches up with my CSS.
However, is it possible to get a width value from Modernizr?
Using built-in tests, nope, that's not possible. At least, there are no mentions in the Modernizr docs.
However, you don't need Modernizr to get the dimensions of your browser. You can do it by using plain javascript:
var width = document.documentElement.clientWidth;
var height = document.documentElement.clientHeight;
Original answer
Related
I was trying to get the screen available height (not include task bars and etc) in Javascript. screen.availHeight in Mac OS/Linux gives me exactly what I need. However, in Windows 10 screen.availHeight returns longer than I expect. It seems to include the length of task bars as well.
How can I get the maximum available height not including the task bars and etc in Windows 10?
So I'm pretty sure using jQuery is the solution!
This code will work quite fine:
$(window).height(); // browser viewport height
Also I'm sure this is not problem of Windows, it's sounds like some kind of browser problem :)
Have you tried to use document.height also? Is this not working either?
$(document).height(); // HTML document height
Try this one:
window.innerHeight;
If you want the height of the window you can do window.innerHeight
If you want the height of the screen you can do window.screen.height
I'm looking at creating a responsive framework for my website - its a first for me. No fancy stuff just responsive through mobile -> tablet -> desktop.
I'm happy with how media queries work, however adding classes through javascript seems like another viable option.
It would be easy to do something like this:
function queryViewport() {
var $window = $(window);
var $body = $("body");
var windowWidth = $window.width();
var windowHeight = $window.height();
// Query window sizes to determine whether device is to be
// classified as a small mobile device or a larger tablet/
// desktop device.
var isMobile = windowWidth < 600 || windowHeight < 600;
$body.toggleClass("mobile", isMobile).toggleClass("desktop", !isMobile);
// Calculate whether viewport is portrait or landscape
var isPortrait = windowHeight > windowWidth;
$body.toggleClass("portrait", isPortrait).toggleClass("landscape", !isPortrait);
}
However, I'm not an expert in this area - am I missing something or is it really that simple?
All advice appreciated.
you can use this minified jQuery snippet to detect if your user is
viewing using a mobile device. If you need to test for a specific
device I’ve included a collection of JavaScript snippets below which
can be used to detect various mobile handheld devices such as iPad,
iPhone, iPod, iDevice, Andriod, Blackberry, WebOs and Windows Phone.
if(jQuery.browser.mobile)
{
console.log("You are using a mobile device!");
}
else
{
console.log("You are not using a mobile device!");
}
See more DETECT MOBILE DEVICES USING JQUERY
See the link below to understand the difference
What is better: CSS media queries or JQuery mobile?
I would suggest media queries, as all future amends can be done in the CSS without adding more and more logic to a separate JS file for new breakpoints.
Additionally, the CSS solution is supported down to IE9+ and there are JS polyfills (Respond) for backwards compatibility. Basically it's just built in to CSS and works well. There seems little point of rewriting the same logic in javascript, having a new class for every new size.
On top of this, media queries allow you to target CSS as different media types such as print, or if you want to use height-based media queries or target retina displays you can do this without having to add new classes. As a rule the convention is to use media queries with JS fallbacks and I see no reason to suggest otherwise.
JS produces different results for detecting viewport heights and widths depending on how you get them:
In that case, you could get screen width using window.outerWidth, window.innerWidth, document.documentElement.clientWidth. All these produce different results, but the second will give you values identical to CSS #media breakpoint.
$(window).width() too, is different from #media breakpoint.
I depends on browser differences, e.g. if the take in account the vertical scrollbar or not. Better go with CSS.
I'm looking for a code that either:
Prevents an element from extending beyond the edge of a window.
OR
Detects the window width or height so that the element's width/height may be set in units of px.
This code only needs to work in Google Chrome.
I've done a LOT of research and everything looks so complicated. Isn't there a simple CSS solution?
Thanks much in advance!
I had a similar problem that jQuery solved for me,
var width = jQuery(window).width();
there's also a .height() method
I am relying on $(window).width(); & $(window).height(); for a resizing function and they seem to come out differently in different browser, same thing is also happening with innerWidth/innerHeight. What is the best way of getting an accurate value for this?
Thanks for any help
Thanks for inpt - for clarification I am using jquery Thanks but I am using 1.7.1 and I have the following css:
*{padding:0;margin:0;border:0;}
with no changes to padding or margin in html or body
$(window).width() is get the browser viewport width, why the ie browser get the less width? because in the left side of the browser, it has a border. so when you use the .width mothed, the ie browser's width will less than chrome\safari\firefox's.
Turns out the documentation clarifies that:
$(window).width(); // returns width of browser viewport
So being the viewport, each browser has a different viewport usable area.
it is a old version jquery bug
Ensure you're using body, html {margin:0; padding:0;} - might be default values in different browsers otherwise.
Ok, so I'm trying to build a website in which a large picture is the background. I want to be able to load a different size picture based on the user's window size so they (hopefully) don't see any blank space. I want to use JavaScript to measure the size of the current screen. Then, based on the size of the window, I would like to load in an image that corresponds to that resolution.
However, when looking online, I could not find a standard way to get the window size in all browsers. Any help is very appreciated! Also, if you have another idea of how to implement this, let me know!
A user's window is unlikely to vary hugely in size during use. It's best to only provide one image, and then scale it depending on the size. This can be achieved through CSS alone.
img#my-big-background-image {
width: 100%;
}
jQuery, YUI, and other JavaScript frameworks have solved this problem. Take a look at their solutions, even if you don't want to plug in a full framework.
Use jQuery
var height = $(window).height();
var width = $(window).width();
That will work in all major browsers.