Solved

show popup onClose page/tab

Posted on 2008-10-15
4
589 Views
Last Modified: 2012-06-27
I need some javascript that will show a popup after user has closed the window(or just before the closing). i have found woring exampes of onUnload onbeforeUnload, but i cant make this works.
Please help.

thanks
0
Comment
Question by:starbones
  • 3
4 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
What type of popup?  The use of onbeforeunload is limited.  It basically will just provide a way for a "confirm" type alert to appear.  You can specify the message so this may be all you need but if you mean a popup window or some element then you won't be able to do this.
If the alert/confirm is what you want then specify the message like ...
window.onbeforeunload = function() { return "Are you sure you want to go?"; };
Let me know if you have a question.
bol
0
 

Author Comment

by:starbones
Comment Utility

It needs to open a popup with survey(so people can select why they leave the mid-order process. So it needs to be shown when user clicks on exit button or when trying to go(browse) to another site without finishing order procedure.

if its not possible to open new popup window, i guess i will have to use onbeforeunload, but then how can i check if user is trying to leave to another domain and show the alert? If they just try to browse to another page of my site, then i should display the aert dialog box.

thanks
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 180 total points
Comment Utility
Most methods that try to tell if a person is just going to another page on your site use a "switch" or variable that is true/false.  By default the value is one that would cause the confirm.  Each link on the page is set up to change that value to the value that would NOT cause the message.  For example ...
var goingOut = true;
function leaving() {
    if (!goingOut) return false;
    return "Are you sure?";
}
window.onbeforeunload = leaving;
Then each link would have some thing like ...
<a href="thenewpage.htm" onclick="goingOut = false;">Another page</a>
Keep in mind what I said about the onbeforeunload event though.  That event is unique and can't be used for a popup or script that would "hold" the page while a form, etc is completed.  The alert type confirm will appear so a user can confirm they want to go and you can set the message but you can't do the popup, etc.  Let me know if you have a question or need more info.
bol
0
 
LVL 54

Expert Comment

by:b0lsc0tt
Comment Utility
I'm glad I could help.  Thanks for the grade, the points and the fun question.
bol
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now