Check if User has clicked "Ok" for "onbeforeunload event" - javascript

I am checking when the user closes the browser window, if he says ok, how i do I trigger some action, like opening another window or saving a form. If he chooses "cancel", it should stay in the same page (which is working now). pls share some idea on how to approach this issue
Sample Code

You cannot react to the user clicking on the "Leave this page" button. This is by design of the browser: if the user wants to leave, let him go.
If the user chooses to cancel, you can try a technique found in this answer to similar question to execute some custom code like saving changes or the like.

Related

Modal popup on window close using javascript/jquery

I have a website where the customers come and purchase any product. But there are some customers who closes the tab without doing any purchase. I don't want to lose those customers. So i have decided to provide offers for those customers in the "modal" popup. This popup has to be displayed once the customer clicks the close button in the browser. This popup will have a form to select the offers, fill their contact details and submit the form. Once the form gets submitted, the window can be closed automatically. Is it possible to provide such a popup modal. I have surfed regarding this. Everyone says about the onbeforeunload with confirm and alert box. Could anyone help me on this.
What you are trying to do cannot be done. That is after the user has clicked on close there is no way to do it.
But I think what you are looking for is what is called a 'Exit intent popup'.
The idea is simple: you use js/jquery to find if the user is 'about to' close the page(say from mouse movements and such) and display the said popup.
Here is one such plugin : BIOEP
You can just google 'Exit intent popups' for many more.
you can't do it from your page code,
only place I see this is in extensions.

Browser Tab Close Event for javascript or jquery

I have added a div which contains some survey questions which is initially hidden. Once the user closes the window or navigate away from the page, I need to popup the survey div to get the confirmation. I know there is a browser default confirm box method by using onbeforeunload event. But I need to popup the div rather than the browser default confirm box. Is there any way to do this? (or is there any way to interrupt the close event without even clicks on the "Stay on page" button found in the browser default confirm box?)
UPDATE : it seems we cannot do the action you are asking. There are some solutions to cover this in IE but overall its not possible. Ignore all the previous code.

Don't allow user to close window for incomplete form

I have a big form on jsp page where user enters some information.
My requirement is that if user move away to another link or close window, I should alert that "Your form changes will be lost if you move away from screen, please press save before moving away from this screen." If user press ok, we should navigate to next page else stay on same page.
How can I achieve this? Any javascript method available for this? Any help much appreciated.

How facebook/twitter share buttons avoid being blocked by popup blockers?

When a user which is not logged in to Facebook or Twitter, clicks the like/tweet button, a popup comes up asking the user to log in. This popup is not blocked by popup blockers of any kind. in IE for example, it opens up but anyway IE state a warning to the user. The bottom line is that all buttons probably use the same method.
I have a button that people click on it and it should open a similar screen, but it always gets blocked.
The button (in JS) works pretty much like FB like button. It checks if the user is logged in. If he does, then send the "like" to the servers. If he does not, then the login popup appears (and gets blocked).
So, it is a initiated by a user although not fully direct outcome, since we need to check if he logged in or not.
What is best method to do that?
The Popup Is activated by a click using the like, tweet button and they actually show only 1 popup. If you want, you can try by using only a single popup based on click for your website and you will definitely be able to understand this.

Pass an alert upon exiting specific domain path

I have been tasked with the following.
Display an alert "You are now leaving the special section of our website" when a visitor leaves a specific directory of our site.
For example.
Visitor comes to our home page www.sample.com.
Clicks link to navigate to "www.sample.com/special-place/"
Clicks another link and navigates to "www.sample.com/no-special-place/"
Anytime the user leaves that section of our site, the message needs to appear.
And no, we can not just show this once (cookie based) it needs to display every time they leave that section of the site. Yes. I know. Horrible user experience.
Could this be accomplished with Javascript by capturing the visitor when they enter the special-place path, then exiting to any other path that does not have the matching characters of "/special-place/"?
Possibly using jQuery to do this?
Any insight or thoughts are greatly appreciated.
Use window.onbeforeunload which will fire a prompt asking the user if they want to leave.
$(window).bind("beforeunload", function() {
//logic if you want to show prompt
return "Are you sure you want to leave";//this text will be shown in a prompt
});

Categories

Resources