How do I perform the action of a specified event before the page binding is refreshed?

The

page structure is divided into three parts: upper, middle and lower, in which there is a iframe . Each time a different link address is sent from the background to show a different interface, this is dynamic.
where the content of the src of iframe is the domain name + the page link from the background to determine the content to be displayed. When I first opened the page, I brought my own page link. I now want to make every time the page is refreshed, it is by passing parameters to the background to call the same method when the page is opened, and then it is tantamount to saying that the page is still open.
the code for refreshing the page I found on the Internet is as follows:

window.onload = function (){
        if(window.attachEvent){
            window.attachEvent("onbeforeunload",onbeforeunload_handler());
        }else{
            window.addEventListener("onbeforeunload", onbeforeunload_handler(),false);
        }
    }

but the problem with this is that the onbeforeunload_handler () method is executed the first time the page is opened, which is tantamount to falling into an endless loop. I have been looking for a solution on the Internet for a long time. Please take a look at it with the help of the god who knows how to solve it. Thank you.

Mar.24,2021

remove the () after onbeforeunload_handler, and add parentheses to execute the function

< hr >

try it yourself, both methods are possible, the difference is that the first binding method will not appear pop-up box reminder. You can test this code alone. It should be fine. It may be something else, or it may have been executed. It just doesn't show the results you expected, or it's not obvious
1
2

.
Menu