Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

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

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

marknadig opened this issue Dec 15, 2011 · 5 comments

Comments

@marknadig
Copy link
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().

@marknadig
Copy link
Contributor Author

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
Copy link

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.

@marknadig
Copy link
Contributor Author

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

@bewest
Copy link

bewest commented Jan 5, 2012

Is this a duplicate of issue #337?

@pfiller
Copy link
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 as completed Jan 12, 2012
pfiller added a commit that referenced this issue Jul 19, 2013
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
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants