Solved

show popup onClose page/tab

Posted on 2008-10-15
4
594 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
ID: 22728129
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
ID: 22728559

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
ID: 22728607
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
ID: 23175315
I'm glad I could help.  Thanks for the grade, the points and the fun question.
bol
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to make a moving character 3 59
PHP Form Calculate Total Price 10 43
Scroll 5 news at a time. 4 28
asp.net mvc5 6 17
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

823 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