5

Setup

I've attached an event to the 'window' object, and I would like to check that it's there via code.

window.addEventListener('beforeunload', function(e){ /*...*/ }, false)

Attempts

I've tried the simple, and jQuery, without luck. I have more attempts up on jsFiddle.

window.beforeunload //is undefined as is window.onbeforeunload

$(window).data('events') //not working either

Is this possible?

There are similar questions (here and here) about the DOM, and other elements, but none of the approaches in those that I have tried have worked.

0

1 Answer 1

10

You can use the in operator...

'onbeforeunload' in window;  // true (if supported)

If supported, the property will exist, though the value will be null.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Works in IE, Chrome, but not FF.
@NickJosevski: Works for me in FF 10. When you say it doesn't work, what do you mean? What result are you getting?
Oh that's right, tested in FF 3.6... Works in 10. Thanks :) Problem still in Opera 11.61 but I can live with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.