Jeremy Stein - Brain

« »

Javascript: on scroll to end

Sometimes software installers force you to scroll to the end of the EULA before the “I agree” box is enabled. If you want the same affect in HTML, you can use javascript like this:

<html>
    <head>
        <script type="text/javascript">
            function setupPage() {
                var agreement = document.getElementById("agreetext");
                var visibleHeight = agreement.clientHeight;
                var scrollableHeight = agreement.scrollHeight;
                if (scrollableHeight > visibleHeight) {
                    var checkbox = document.getElementById("agreebox");
                    checkbox.checked=false;
                    checkbox.disabled=true;
                    agreement.onscroll = handleScroll;
                }
            }

            function handleScroll() {
                var agreement = document.getElementById("agreetext");
                var visibleHeight = agreement.clientHeight;
                var scrollableHeight = agreement.scrollHeight;
                var position = agreement.scrollTop;
                if (position + visibleHeight == scrollableHeight) {
                    document.getElementById("agreebox").disabled=false;
                }
            }
        </script>
    </head>
    <body>
        <form>
            <textarea id="agreetext" rows="8" cols="40" onscroll="calc();">Long agreement</textarea>
            <br/><br/>
            <input type="checkbox" id="agreebox" value="true"/> <label id="agreelabel" for="agreebox">I agree</label>
            <br/><br/>
            <input type="submit" value="Continue"/>
        </form>
        <script type="text/javascript">
            // We put this at the end of the page rather than in an document.onload
            // because the document.onload event doesn't fire until all images have loaded.
            setupPage();
        </script>
    </body>
</html>

See it in action.

September 8, 2008 No Comments.

No Comments

Be the first to comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?

« »