How to submit a form when page loads using javascript?

I know this question has been answered many times but in my case the JavaScript code creates a infinite loop which makes my page load continuously,


    window.onload = function(){
        document.getElementById('theForm').submit();
    }

I also tried the below one and it doesn’t work.

<body onload="document.theform.submit()";>

The below code does what I need but it’s not perfect.


    window.onload = function(){
        document.getElementById('submit_button').click();
        setTimeout(function(){window.stop();},2500);
    }

How to submit a form when page loads using javascript?

I know this question has been answered many times but in my case the JavaScript code creates a infinite loop which makes my page load continuously,


    window.onload = function(){
        document.getElementById('theForm').submit();
    }

I also tried the below one and it doesn’t work.

<body onload="document.theform.submit()";>

The below code does what I need but it’s not perfect.


    window.onload = function(){
        document.getElementById('submit_button').click();
        setTimeout(function(){window.stop();},2500);
    }