Why are we supposed to prefer document.visibilityState to document.hidden?
Asked Answered
E

2

9

I was looking at the document.hidden specs here to understand what exactly hidden entails:

  1. If steps to determine the visibility state return visible, then return false. 2. Otherwise, return true.

Note

Support for hidden attribute is maintained for historical reasons. Developers should use visibilityState where possible.

visibilityState is another property that can return one of three strings: 'visible', 'hidden', or 'prerender'.

document.hidden (as indicated by the above rules) returns true for 'visible' and false for all other strings.

I am curious about the note. Why do they say that developers should prefer visibilityState to hidden?

Is it simply because visibilityState gives you more "power" (re: granularity) in that it's more specific about what state the document is in, whereas hidden dumbs it down by turning the four states into two states, "hidden" or "not hidden"?

I understand that this may only be answerable by the people who wrote the specs, but maybe they're crawling around here.


A preemptive note: I'm going to guess that this may be closed as "opinion-based", because it'll attract opinionated answers, and while it certainly may attract answers like that, this can be answered objectively by those who wrote the specs, so I'll take my chances.

Ern answered 13/6, 2018 at 22:1 Comment(5)
As an update to this question, where you mention the four visibilityState possible values, I would like to note that the 'unloaded' value has become deprecated and thus only three possible values for this property remain.Masterly
@RobinBastiaan Thanks, edited itErn
Some discussion: github.com/w3c/page-visibility/issues/63Amused
@Amused Thanks! looks like this note about not using hidden was since removed. Would you like to post an answer?Ern
Three years later, it looks like there are now only two possible values for visibilityState, 'visible' and 'hidden'. See same link to MDN docs for visibilityState by OPEmpale
E
2

The notice of hidden being maintained for historical reasons has been removed in this PR.

There is some discussion here about this change.

Ern answered 15/5, 2021 at 14:42 Comment(0)
D
1

I did not write the specification, but I would assume you are correct -- the introduction of visibilityState made hidden obsolete. hidden does nothing that visibilityState doesn't do better. It is likely on the road to deprecation, given their insistence that you use the latter.

Daugavpils answered 13/6, 2018 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.