I have a button that calls a function onclick. The purpose of the button is to assign a function to window.onclick. The problem is that the function that I have assigned to window.onclick, just by pressing the button, runs immediately… And that’s not what I’m trying to accomplish. As far as I know im not calling the function, im just assigning it to the window.onclick by reference or whatever you prefer to call it.
This is what I want to happen:
The first press on the button should just “active” the function. Not call it immediately. Why does this happen with the code below? Its like I activate the “window onclick function”, and then it reacts directly to the previous click on the button.
However, when I change onclick to onmousedown it works the way I want it. I want to understand why it runs immediately without me calling it.
Here is the code:
<button onclick="activateWindowOnClick()">ACTIVATE</button>
function activateWindowOnClick () {
window.onclick = windowfunction;
}
function windowfunction() {
alert('This should not be displayed now. I have not clicked on the window after activating the function');
}
Bless