kwyjibo
kwyjibo

Reputation: 557

How do I get access to the <html> element in Javascript?

IE displays a default scrollbar on the page, which appears even if the content is too short to require a scrollbar.

The typical way to remove this scrollbar (if not needed), is to add this to your CSS:

html {
  height: 100%;
  overflow: auto;
}

I'm trying to do the same thing in Javascript (without requiring that in my CSS), but I can't seem to find a way to get access to the <html> element. I know I can access the <body> element with document.body, but that doesn't seem to be sufficient, I need the wrapping <html> element.

Any tips?

Upvotes: 1

Views: 138

Answers (3)

eyelidlessness
eyelidlessness

Reputation: 63529

I guess for completeness' sake, I'll add another way to access it:

document.getElementsByTagName('html')[0];

Obviously this is a little more verbose, but it's always going to work regardless of the structure or standards-mode of your document.

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382806

You can also reach the HTML element with:

var html = document.body.parentNode;
alert(html.nodeName);

Upvotes: 0

SLaks
SLaks

Reputation: 887777

You're looking for the document.documentElement property.

Upvotes: 2

Related Questions