Skip to content

[BUG] semantics of (onbeforeunload, Dom_html.handler) incompatible with major browsers #1436

@erikmd

Description

@erikmd

Describe the bug
Adding/removing hooks for the beforeunload event with Dom_html.window##.onbeforeunload := Dom_html.handler … is not working out-of-the-box with both Chromium, Firefox, and Safari. So I had to devise the following hack (unsafe JS code):

  let prompt_before_unload () : unit =
    Js.Unsafe.js_expr "window.onbeforeunload = function(e) {e.preventDefault(); return false;}" in

  let resume_before_unload () : unit =
    Js.Unsafe.js_expr "window.onbeforeunload = null" in

  (…)
  if not sync then prompt_before_unload () else resume_before_unload ()
  (…)

Source code: https://github.com/ocaml-sf/learn-ocaml/blob/a6e4c5e6/src/app/learnocaml_exercise_main.ml#L292-L310

Expected behavior
I open this PR in case it is possible for you to tweak Dom_html.handler, and be able to directly use js_of_ocaml's abstractions (not Js.Unsafe.js_expr) for this use case, and stay compatible with (Chromium, Firefox, Safari…)

FTR → I had sketched a possible explanation of why one such tentative code doesn't work in Firefox here:
ocaml-sf/learn-ocaml#467 (comment)

Anyway, if ever you'd think it is already possible to implement the snippet above without Js.Unsafe.js_expr (meaning there's no "bug" over there), I'm taker of any idiomatic snippet 👍

Versions
Version of packages used to reproduce the bug:

  • ocaml 4.12.1
  • js_of_ocaml 3.9.0

(I'll be happy to provide more details on the versions used if need be)

Cc @yurug FYI

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions