Determining Document Height with JavaScript
Obtaining the correct height of a document in JavaScript can be a challenge due to browser compatibility issues. While all browsers provide properties like clientHight and scrollHeight, their calculations vary.
Fandango and Paperback Swap Example
On websites like Fandango and Paperback Swap, traditional methods such as $(document).height(), document.height, and document.body.scrollHeight fail or return inaccurate values.
Best Practice: Maximum Height Calculation
To address this, a best practice has emerged: determine the maximum height from all available sources. This includes:
Code Implementation
The following code sample implements this approach:
var body = document.body, html = document.documentElement; var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
Considerations
The above is the detailed content of How Can I Reliably Determine Document Height in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!