window beforeunload triggered from chosen click, IE 8 & 9 #403

marknadig opened this Issue Dec 15, 2011 · 5 comments


None yet

3 participants


I am using sammy.js in my app and found that in IE8 and 9 it would sporadically stop working. I tracked it down today to chosen causing 'beforeunload' on window to be triggered when the dropdown is clicked. Sammy is bound to that and unloads itself. It's not clear to me what is causing beforeunload to trigger.

To reproduce, just open test page in IE8 or 9, start developer tools (f12) and click 'start debugging' on script tab. In console bind to 'beforeunload':
$(window).bind('beforeunload', function() {alert('in before');});

and then click on any of the chosen dropdowns and see alert.

any help appreciated.

after some googling found some threads re: IE triggering beforeunload and a suggestion to return false from the click handler to prevent this. I confirmed that changing Chosen.prototype.container_click() to return false resolved this behavior in IE - or better still, calling preventDefault() on the evt passed into click().


Just more info...

I believe this is all tied to the fact that container_div is an anchor and any anchor click in IE is going to trigger beforeunload. So, def seems best to return false from always

also see in latest source that there is no longer a click handler, rather just mousedown. So, problem exists in current branch - I'll work on a patch.

ghost commented Jan 5, 2012

Even after returning false the beforeunload event is getting triggered in IE8.

For IE9, adding 'onclick="return false;" ' in markup for '<a href='javascript: void(0)'...' element avoided this issue.

returning false in container_mousedown function did not help either for IE8.

Did you find another solution or better hack for this?



Hi, yes, my pull request has the fix I found - evt.preventDefault() in click handler. Should work for you, let me know. thanks.

bewest commented Jan 5, 2012

Is this a duplicate of issue #337?

pfiller commented Jan 12, 2012

Thanks again, @digger69. Your fix has been merged as of a few moments ago.


@pfiller pfiller closed this Jan 12, 2012
@pfiller pfiller added a commit that referenced this issue Jul 19, 2013
@pfiller pfiller Replace `javascript:void(0)` with `#`
- issues with content security policy (#1215)
- onbeforeunload in IE (#403)

We don't need to add a preventDefault because all clicks on
.chzn-container already have a preventDefault
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment