2

I've recently been given this piece of javascript (as part of a jQuery function)...

var h = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
var w = "innerWidth" in window ? window.innerWidth : document.documentElement.offsetWidth;

I've never seen "innerHeight" in used before, and I cannot find any explanation of what it is for, or why it's used. (Doing a Google search for the word in isn't exactly helpful.)

Is it jQuery syntax?

10
  • I've put "javascript in" in google and it provided a bunch of relevant results. Haven't you heard of google.com? Commented Apr 18, 2014 at 9:43
  • @KarlenKishmiryan ,plese , OP gets nervous when he sees tag POSSIBLE DUPLICATE OF ............. .He didnt do any crime :( Commented Apr 18, 2014 at 9:44
  • @Pratik Joshi: they did actually. They didn't bother trying to solve it themselves. Commented Apr 18, 2014 at 9:45
  • @Amit Joki: excuse me? No it's not. Why are you asking? Commented Apr 18, 2014 at 9:45
  • @zerkms, that was a joke ;) Commented Apr 18, 2014 at 9:46

4 Answers 4

5

No, the in operator is core JavaScript.

It resolves as true if the object on the right hand side has a property with a name that matches the string on the left hand side.

See a simple example:

var foo = {
    a: 1,
    b: undefined
}

var o = document.getElementById('output');

o.innerHTML = ("a" in foo) + "<br>" + 
    ("b" in foo) + "<br>" + 
    ("c" in foo) + "<br>";
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Quentin - as zerkms "helpfully" points out in their comment under the question, I didn't search for the correct thing - your explanation is excellent
2

This is pure JS.

This is similar to object.property with a little difference.

object.property will fail in cases property is not set and also in cases it has been set to some false value, e.g. undefined, null, 0 etc

property in object is slightly better because it doesn't looks for its value.

Comments

2

Check if a object's property exists.

in is used to check if an object contains a specific property , this is rappresented by a string.

var obj={a:1,b:2}

// in
console.log('a' in obj); // true
console.log('c' in obj); // false

Said that.. there is a faster/shorter way to do that.

// this does not work if the property's value is 0,null,undefined,false...
console.log(!!obj.a); // true  
console.log(!!obj.c); // false

demo

http://jsfiddle.net/cLDEd/

in your case a better solution would be:

var H=window.innerHeight?window.innerHeight:document.documentElement.offsetHeight

or even better :

var H=window.innerHeight||document.documentElement.offsetHeight

Theoretically window.innerHeight could be 0 , so at that point it would try to get the document.documentElement.offsetHeight whitch returns undefined and you should add another or ||0 to return 0 and not undefined. But that does not happen ;).

Putting window.innerHeight in front of document.documentElement.offsetHeight accelerates the check as window.innerHeight is standard, so there is a higher chance that it's the correct choice.

Comments

0

You can see it as if innerHeight <inside> window then window....

1 Comment

What inside term means?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.