Fix outerHeight to OR with 0 to not produce NaN.

This is an issue where in jQuery v3 the result of outerHeight on a node
that doesn’t exist is now “undefined” rather than “null”, which means
it will no longer cast to a Number but rather NaN. For this, we create
the safeOuterHeight and safeOuterWidth functions to safely return a
result (or 0).

This is a better solution than manually going to each instance and
ORing it with 0 for type safety.

https://stackoverflow.com/questions/41454285/jquery-outerheight-returns-
undefined-instead-of-null
This commit is contained in:
Brock Whittaker
2017-07-27 12:40:26 -07:00
committed by Tim Abbott
parent ac4ac63353
commit d01549e8e3
6 changed files with 36 additions and 22 deletions

View File

@@ -9,7 +9,7 @@ $(function () {
function scrollbarWidth() {
$('body').prepend('<div id="outertest" style="width:200px; height:150px; position: absolute; top: 0; left: 0; overflow-x:hidden; overflow-y:scroll; background: #ff0000; visibility: hidden;"><div id="innertest" style="width:100%; height: 200px; overflow-y: visible;">&nbsp;</div></div>');
var scrollwidth = $("#outertest").outerWidth() - $("#innertest").outerWidth();
var scrollwidth = $("#outertest").safeOuterWidth() - $("#innertest").safeOuterWidth();
$("#outertest").remove();