arpan
Hello everyone,
I’m currently working on a page that features a live view, and I’m aiming to prevent users from accidentally leaving the page. To achieve this, I’m utilizing the beforeunload browser event in conjunction with a Phoenix hook. The structure of the hook is as follows:
export default function preventPageLeave() {
const beforeUnloadListener = (event) => {
// Prevent navigation and display a confirmation dialog
event.preventDefault();
event.returnValue = '';
};
return {
mounted() {
window.addEventListener("beforeunload", beforeUnloadListener, { capture: true });
}
};
}
This approach works seamlessly; however, I’m encountering a challenge when dealing with links on the page that use the link/1 function. For redirects managed by the Phoenix socket, which don’t necessitate a full page reload like navigation or patch operations, the beforeunload technique doesn’t prove effective.
Regrettably, I haven’t yet found an elegant solution to address this particular issue. I would greatly appreciate any insights or suggestions you might have.
Thank you in advance for your assistance!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
regex.sh
Maybe you could assign a class to
linkand then add event listener to all classes on the page?arpan
Thanks for the reply, yes I tried this approach already something like this
On mount, I could do
This works but I believe it is not the best idea, for example the live view might add new links to the dom on a certain user interaction which would not have the event listener and would therefore fail to intercept the event.
I feel like this is a common use case and there should be a elegant way to handle it.
regex.sh
Another option is If you are changing URL then you can listen for hashchange_event or popstate_event
Another option is MutationObserver
carlgleisner
Following up on this topic – has there been any development on this?
I’m also using
beforeunloadto help my users avoid shooting themselves in the foot. The live navigation part is still blindspot.@arpan, did you have any success with @regex.sh 's suggested approaches?
I hope that many would appreciate the bump of this thread if it’s possible to increase the ergonomics in this regard
arpan
This is the hook that I use right now