Popup message when leaving page - javascript

I am looking to develop a small popup message which acts similar to the window.beforeunload function, to notify the user, that if they leave the current page, they will lose all of their data.
However the issue with the beforeunload event is it fires to often.
I would like to have the popup message fire only when a user closes the page, or clicks a link which takes them away from the current page, to ensure they are aware that their current action will result in the loss of the form data they have entered so far.
However beforeunload event goes further to fire when they refresh the page, which is not needed for this case, and also when the forum is submitted.
Could anyone advise me on the best way to develop this. I thought about using a basic confirm dialog and have it fire under the right circumstances, however is it possible to know if the user is refreshing the page, and if the forum is being submitted (without jQuery).
How can I have this dialog fire at the appropriate times?

Unfortunately, I don't think this is possible. The page unload events are very limited, for security reasons.

If you only want it to appear if the user added or changed formdata, why not check for changes in the data? If yes then return the question on beforeunload, if not do nothing.

Assuming that the form isn't too complicated, you could save form data by using Ajax call, which means there will not be a page reload. So, beforeunload will then behave as it was designed to.

Related

call different function during page refresh and close javascript

I am creating a web app using web socket, which on user closes the tab I will make an API call to the server to clean the user related info in the server, I used onBeforeUnload listener in javascript, but this method also gets triggered during the page refresh.
I need to trigger a method only during the tab or browser close, but not during the page refresh.
I know this question has been asked several times, some solution suggested using cookies will not be helpful in my case
navigator.sendBeacon() method can be used for sending data from browser to server when a tab is closed.
Here is an example:
window.addEventListener("unload", function informServer() {
navigator.sendBeacon("/server-api-to-collect-data", my-data)
})
More information:
https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon
As far as I know, you can not listen to actions of browser's tab close or exits. For your application it is an "unload", whatever caused it...
The only thing I could think about is maybe add a listener for keyboard key press (F5), however it doesn't help in case someone refreshed by clicking on the browser's refresh button.
I don't know what is the use case, but most of the things should be done when a page unloads (no matter why) and/or when the page is back up again. So most of the solutions are for situations where your page is loaded again - and then you can determine what was the source of the load and make farther actions, but since you have an option were someone can close and never come back, that might not be the case.
Some solutions for page load up:
You can use Navigation type.
You can check referer.
You can use cookies or other types of browser storage.
I would recommend to rethink about your use case. Maybe you can do whatever you want on load up or leave it on onBeforeUnLoad without knowing the future :)

Using Javascript is it possible to hook a custom function to the browser back button while preventing the default event?

Using Javascript is it possible to hook a custom function to the browser back button while preventing the default event?
So simply put, the user clicks the browser back button, and instead of being taken back to the previous page on my site, my function fires. My aim is not to to prevent the user leaving my site.
Something like the below pseudo code:
window.backbutton.click(function(e){ preventDefault(e); myFunc(); })
FYI What I actually have is an internal back/forward system controlled by buttons within the view and I'd like to trigger my buttons when user clicks browser back.
You should look into Browser History https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history . It is a new standard for emulating pages (back and forward) on a single-page site.
you can do this
$(window).bind("beforeunload", function() {
return "Are you Sure you want to do this even though there are internal navigation buttons? if you do whatever you have done so far
will be gone unless it has been completed and/or submitted "; //
Return whatever message you want
});​
Edit: this method displays a confirmation with the message and
automatically places buttons in the confirmation window
the author also clearly stated he does not want to prevent users from
leaving his site
see js fiddle http://jsfiddle.net/JW9fX/
Aaron - "updated question, I do not want to prevent them leaving my
site – Aaron 27 mins ago"
Edit: there is no definitive way to display a message only on the back
button in my knowledge i have also looked and have seen the only
methods that pick up on the back button are
$(window).bind("beforeunload",function(){return;}) and
$(window).onbeforeunload = function(){return;} and they both pick up
on when the page is refreshed or any other navigation away from the
current page. This is currently the best answer that can be provided
in the year 2014
Edit:
above is for all unload events
http://jsfiddle.net/Lhw2x/85/
been messing with that through all its lower objects it cant be done through javascript till someone decides to make all major browsers use a specific name for each unload event instead of it just being an unload event
so the answer is no you cannot hook a custom function to the browser back button while preventing the default event using javascript

jquery onbefore unload trigger

I'm using a script to fade out the page content when user is actually leaving the page.
For that I tried using the beforeunload event.
It works fine when I navigate through my site, however I also found it triggering on application launch, such as skype button. Even though I never left the page yet...
What is the best way to validate onbeforeunload event against premature triggering?
I guess I should still use my:
$(window).one('beforeunload', function() {
// need to make some condition, if really leaving the page - execute
$('html.nojs').stop(true,false).css('overflow','hidden').animate({opacity:0},2000);
});
But I would need to use some condition... just cannot think of any...
EDIT:
Rmoved the link to the site
The animation does work, but if you go to any specific product and click skype button there you will see the it makes poo...
EDIT2:
The solution to this problem is to detect what triggered an before unload event. As I said, by writing some condition inside that call.
We must find a way to find out whether the event was triggered by external application call (such as skype button that tries to open application) or was it something else, like... for example:( link click, a button submit, script for location change, starting a search, hitting back/forward, or refreshing the page).
This the accepted answer for this question
Capture user response when using window.onbeforeunload
If you need to know what triggered the onbeforeunload event, you can have a global variable, then set it to true when you click on the Skype button. Then check it inside your onbeforeload event.

How to capture browser's event by javascript

i want to know is there any way we can know browser's events.. like : clicking on BACK button, FORWARD button, REFRESH button by javascript.
These specific browser events are not available as it would be vulnerable to severe privacy violations. Privacy is something browser vendors hold sacred and a key selling (proverbial) point. All browsers allow you to know is when a user enters or leaves your page for which Kamui pointed out the technical details.
Within the same site, it's possible to achieve some browser event tracking using cookies and javascript. For example track wether users click on a hyperlink and label it as a forward event and when a user leaves the page without clicking on a hyperlink it could be one of:
browser url input
back action
javascript location.href replace
The location.href replace can be tracked as well when you have full control over all javascript, just use a helper method with tracking code instead of directly chaning location.href.
That leaves browser url input and the back action. With cookies and request headers (getting the referrer) it is possible to get close to finding out the forward and back events, though not 100%, but pragmatically, 99% sure is good enough.
Figuring out the refresh event is easy with request headers (referrer), if the current url matches the referrrer, it's a refresh event.
Now I offer no code or definite solution, but I outlined what you could do to track back, forward and refresh events within a single domain context. It won't be a quick and easy way to implement it and as far as I know, there's no framework in existance that reliably tracks browser events or even comes close to what I described above.
A more common/lazy technique to achieve something similar is to create a single page app, for which there are many frameworks available. Just google single page app framework, but thats a pretty heavy solution with other implications that I won't go into now.
You can not capture (for example run some piece of code when user presses Back button) them, however, you can direct your pages in history by using:
history.go
history.back
history.forward
More about JS History object.
As #sarfraz says you cannot capture the back and forward button clicks but you could call
window.onbeforeunload = function(){alert("you just tried to leave the page");};
which should be triggered when either the back/forward/refresh buttons are clicked to perform an action, unfortunately you can't tell if they are going back or forward. Please note don't alert a message it's really annoying when trying to exit a page.
EDIT
you can also do this in jQuery if you have it
$(window).unload( function () { alert("Bye now!"); } );

How do I open a form on page exit

I'm now dealing with a client request to open a form when the user leave the page.
Can that be done?
I think you are looking for
onbeforeunload event
An event that fires before the unload
event when the page is unloaded.
It seems to be impossible to create my own form and override onbeforeunload or anything else. I guess blocking it is the right thing though..
What we did is to pop a form and use the regular onbeforeunload. if the user wants to go - he will. But if he stays - he will get the questionnaire.
You can see the issue here too: onbeforeunload confirmation screen customization

Categories

Resources