1

I am trying to send a window.postMessage to an iframe on "onBeforeUnload" event. This is to send some session data to the iframe at the end of the session. The iframe is able to successfully receive the message in its message handler. This works for all kinds of onBeforeUnload event such as reloading a page, closing a page, navigating to a diff page by clicking a link.

The issue is when a user is typing a different url in the address bar and navigating to a diff page, the "onBeforeUnload" event is triggered, but the postMessage method is not sending the message. It is really weird to me. I tried the same scenario in other browsers (safari, firefox, opera) and it works as expected (sending postMessage when user types a url in address bar)

If the issue is not clear, i am happy to include a demo fiddle or screenshots.

Thanks

Selva
  • 33
  • 5
  • 1
    A jsfiddle would probably help demonstrate issue. – guest271314 Sep 28 '16 at 22:55
  • 1
    I created a plunkr to explain better. https://plnkr.co/edit/h22xabr2ISU6KL4DZENM?p=preview But you will get a much better understanding when you download the code and run locally. – Selva Sep 29 '16 at 00:12
  • Can you include `html`, `javascript` at plnkr at original Question? Have not yet checked [`beforeunload`](https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload) documentation? – guest271314 Sep 29 '16 at 00:40
  • Have you tried using `history`? – guest271314 Sep 29 '16 at 00:44
  • The plnkr has those files. It has two `html` files one of them is the `iframe`. `beforeUnload` works fine. Its the `postMessage` not sending at the point. if you add a console.log in beforeUnload, you will see that printing in the console. And what so you mean by `history`? – Selva Sep 29 '16 at 01:17
  • You can try using `history` entries to communicate between browsing contexts by storing data at `state` object of `history`, see [Global Variable usage on page reload](http://stackoverflow.com/questions/29986657/global-variable-usage-on-page-reload/). See also using `storage` event [Can the mechanism that loads the worker in Shared Web Workers be modified?](http://stackoverflow.com/q/38938039/) , [Can we refer to javascript variables across webpages in a browser session?](http://stackoverflow.com/q/36146595/) – guest271314 Sep 29 '16 at 03:20
  • To check if `location.href` changes [Javascript: How to intercept window.location change](http://stackoverflow.com/questions/2073086/javascript-how-to-intercept-window-location-change) – guest271314 Sep 29 '16 at 03:29
  • Have you found any solution to this issue? I'm having similar issue where main page is posting the message but iframe doesn't seem to have a chance to handle it when navigating via address bar. – blissfool Feb 22 '17 at 18:00

0 Answers0