summaryrefslogtreecommitdiffstats
path: root/dom/tests/browser/beforeunload_test_page.html
blob: 12dc636e23514b2f06872a43eb1d011677c0b1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!doctype html>
<html>
<body>

<p>
  There are currently <span id="totalInnerHandlers">0</span> beforeunload handlers registered in this frame.
</p>
<p>
  There are currently <span id="totalOuterHandlers">0</span> beforeunload handlers registered on my subframe.
</p>

<iframe src="about:blank" id="subframe"></iframe>

<script>
  this.BeforeUnloader = {
    _innerEventHandlers: [],
    _outerEventHandlers: [],

    get $totalInner() {
      delete this.$totalInner;
      return this.$totalInner = document.getElementById("totalInnerHandlers");
    },

    get $totalOuter() {
      delete this.$totalOuter;
      return this.$totalOuter = document.getElementById("totalOuterHandlers");
    },

    get $subframe() {
      delete this.$subframe;
      return this.$subframe = document.getElementById("subframe");
    },

    pushInner(howMany) {
      for (let i = 0; i < howMany; ++i) {
        let func = () => {};
        this._innerEventHandlers.push(func);
        addEventListener("beforeunload", func);
      }

      this.$totalInner.textContent = this._innerEventHandlers.length;
    },

    popInner(howMany) {
      for (let i = 0; i < howMany; ++i) {
        let func = this._innerEventHandlers.pop();
        if (func) {
          removeEventListener("beforeunload", func);
        }
      }

      this.$totalInner.textContent = this._innerEventHandlers.length;
    },

    pushOuter(howMany) {
      if (!this.$subframe.parentNode) {
        throw new Error("Subframe I'm holding a reference to detached!");
      }

      for (let i = 0; i < howMany; ++i) {
        let func = () => {};
        this._outerEventHandlers.push(func);
        this.$subframe.contentWindow.addEventListener("beforeunload", func);
      }

      this.$totalOuter.textContent = this._outerEventHandlers.length;
    },

    popOuter(howMany) {
      if (!this.$subframe.parentNode) {
        throw new Error("Subframe I'm holding a reference to detached!");
      }

      for (let i = 0; i < howMany; ++i) {
        let func = this._outerEventHandlers.pop();
        if (func) {
          this.$subframe.contentWindow.removeEventListener("beforeunload", func);
        }
      }

      this.$totalOuter.textContent = this._outerEventHandlers.length;
    },

    reset() {
      this.popInner(this._innerEventHandlers.length);
      this.popOuter(this._outerEventHandlers.length);
    },
  };
</script>

</body>
</html>