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

Closed
marknadig opened this Issue Dec 15, 2011 · 5 comments

Projects

None yet

3 participants

Contributor

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 harvesthq.github.com/chosen 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.

-- UPDATE
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().

Contributor

Just more info...
http://stackoverflow.com/questions/5047097/ie8-what-is-tiggering-beforeunload-event
http://answers.microsoft.com/en-us/ie/forum/ie8-windows_other/ie8-fires-onbeforeunload-when-clicking-a/0ac8cd9b-7483-4e16-b7c4-346df1f1499a

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 container.click 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
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?

Thanks.

Contributor

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?

Contributor
pfiller commented Jan 12, 2012

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

3b3eca5

@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 `#`
Fixes:
- 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
4885cb3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment