Explicit way to check whether a function was called from within the Window
Just check if this
is window
:
function get_caller() {
if (this === window) {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}
btn.addEventListener('click', get_caller)
get_caller()
<button id="btn">Get caller</button>