11

I have a problem with my website. On loading it scrolls automatically to the bottom.

It is because there is a contact form with focus on name. How can I remove this focus?

Site

0

2 Answers 2

24

Change this line from the contact from

var contactForm = new VarienForm('contactFormNew', true);

with

var contactForm = new VarienForm('contactFormNew', false);

The second parameter means "focus on first field". That's why you get the down scroll.

1
  • Solve my issue scroll to bottom.
    – Sourav
    Sep 18, 2016 at 13:16
1

If you work for a company that outsources back end development, as I do, you might not be able to access the form template. In this case you won't be able to change the VarienForm function's arguments. (If you can change these arguments, as per the approved answer, you should.)


You can add the code (provided below) to the body of your affected page within a <script> tag.

To summarise, the code waits for the first time the focused element changes, then unblurs that element and resets the scroll to the top of the page. If nothing happens within ten seconds of the script running, it'll turn itself off. This is just a safeguard to prevent the script running forever in the event of an error.

//KILL AUTOFOCUS
(function(){
    var lastActiveElement = document.activeElement;
    //time elapsed in MS
    var time = 0;
    //stop checking after 10 seconds
    var stopTime = 10000;
    //interval to check for changes
    var intervalTime = 20;

    function _Check_For_Form_Validation(){
        if ( document.activeElement != lastActiveElement ){
            document.activeElement.blur();
            document.documentElement.scrollTop = 0;
            clearInterval( interval );
        }
        if ( time >= stopTime ){
            clearInterval( interval );
        }
    }

    var interval = setInterval( function(){
        time += intervalTime;
        _Check_For_Form_Validation();
    }, intervalTime);

})();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.