Search code examples
javascriptbuttonauthenticationclickauto

Automatically click button on page load


I'm a little stuck on creating an automatic page to press a 'log in' button which runs a javascript, submitting the form automatically isn't good enough as it simply doesn't log the user in (it's a javascript login system - as a test), instead, it will just return an error: http://prntscr.com/m1f1f6 it needs to automatically on load 'press' the log in button: http://prntscr.com/m1f1ur in order to perform the action of sending to "./main.php" which has an 'isLoggedIn' function.

<button class="btn btn btn-block" id="btnLoginMain">Log in</button>

Thanks in advance!


Solution

  • First, this is not a good approach to make the customers/users to logging in. You can use cookie or sessions to auto login your user. Still, if you want to click the button auto on web page loading you can use the below script.

    <script>
    window.onload=function(){
    $("#btnLoginMain").click();
    }
    </script>
    

    use this in the header file of your html file. If you want to click the button after some time of page load use the setTimeout() function.