Solved

Open POP UP on window close

Posted on 2009-06-29
14
752 Views
Last Modified: 2013-12-08
I need code , compatible both with IE and Firefox ,for opening a pop up  window with out any alerts if visitors is closing widow with out clicking any where on page .
0
Comment
Question by:Muhammad Wasif
  • 6
  • 5
  • 3
14 Comments
 
LVL 15

Expert Comment

by:fsze88
ID: 24742859
try this
<script type="text/javascript">
 
  if (navigator.userAgent.toUpperCase().indexOf('MSIE')>=0){
    browserType = "MSIE";
  } else if (navigator.userAgent.toUpperCase().indexOf('SAFARI')>=0){
    browserType = "SAFARI";
  }else if (navigator.userAgent.toUpperCase().indexOf('OPERA')>=0){
    browserType = "OPERA";
  }else if (navigator.userAgent.toUpperCase().indexOf('FIREFOX')>=0){
    browserType = "FIREFOX";
  }else{
    browserType = "";
  }
 
function unloadWindowEvent(){
  //alert("abc");
  window.open("http://www.google.com","abc");
}
 
if (browserType == "MSIE"){
  window.onunload = function(){
  window.open("http://www.google.com","abc");
}
}else if (browserType == "FIREFOX"){
  window.addEventListener("unload", unloadWindowEvent, false); 
}
</script>

Open in new window

0
 
LVL 4

Expert Comment

by:Adam Chan
ID: 24743193
and here is the bit about not clicking anywhere.
<head>
<script language="javascript" type="text/javascript">
var clickedAnywhere = false;
function clickAction() {
  clickedAnywhere = true;
}
 
function popupOnClose() {
  if (!clickedAnywhere) {
    // popup code
  }
}
</script>
</head>
 
// onclick event on body tag
<body onclick="clickAction()">

Open in new window

0
 
LVL 20

Author Comment

by:Muhammad Wasif
ID: 24743438
Hi addamez:

I can not use body tag. How can I call this with out using body tag using java script only.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 15

Expert Comment

by:fsze88
ID: 24743644
...... comment 24742859 is works on ie,ff without using body tag........
0
 
LVL 4

Expert Comment

by:Adam Chan
ID: 24743889
To fsze88:

 Your code works perfectly fine, just that it did not cover all of the specifications.
quote wasifg
"opening a pop up  window with out any alerts if visitors is closing widow with out clicking any where on page"


To wasifg:

I don't know the code off the top of my head, you could possibly add a clickEvent listener which calls a function that adds the unloadWindowEvent listener as per fsze88's code.
So if the user clicks anywhere and then close, the unloadWindow event is triggered.

fsze88 can probably help you with that because I am not too familiar with browser events.

Cheers
0
 
LVL 15

Expert Comment

by:fsze88
ID: 24744006
addamez,
Okey, I should have some misunderstanding of this question. Please follow this question.

wasifg,
Please don't split points to me, because I don't want to be accept/assist in this question and I having enough points for this month already.
I think addamez should able to help you!

https://developer.mozilla.org/en/DOM/element.addEventListener may help both of you
0
 
LVL 20

Author Comment

by:Muhammad Wasif
ID: 24751483
addamez :
How can I call these without using body tag . I also need to pass a string from parent window URL to this pop up. Can you help mw with this too?
0
 
LVL 4

Expert Comment

by:Adam Chan
ID: 24751783
give this a try..
i took part of fsze88's code and modified it to get this

as for passing a string to the popup, assuming you are using php, you can modify the url in the window.open function to something like..

window.open("http://www.mywebsite.com/popup.php?string=some_text");

then in the popup.php file you can use $_GET['string'] to retriev the content from the url.

if you cannot use php, you can put javascript in the popup page and use location.href to get the whole url as a string, and extract the necessary bits you need via string manipulation

<script type="text/javascript">
var url = location.href;
</script>

consider using the string.split() method.
<script language="javascript" type="text/javascript">
 
if (navigator.userAgent.toUpperCase().indexOf('MSIE')>=0){
  browserType = "MSIE";
} else if (navigator.userAgent.toUpperCase().indexOf('SAFARI')>=0){
  browserType = "SAFARI";
}else if (navigator.userAgent.toUpperCase().indexOf('OPERA')>=0){
  browserType = "OPERA";
}else if (navigator.userAgent.toUpperCase().indexOf('FIREFOX')>=0){
  browserType = "FIREFOX";
}else{
	browserType = "";
}
 
function init() {
	if (browserType == "MSIE"){
		window.attachEvent('mousedown',clickEventHandler);
		window.attachEvent('unload',unloadWindowEvent);
	} else if (browserType == "FIREFOX") {
		window.addEventListener('mousedown',clickEventHandler,false);
		window.addEventListener("unload", unloadWindowEvent, false); 
	}
}
 
function clickEventHandler() {
	if (browserType == "MSIE"){
		window.detachEvent('mousedown',clickEventHandler);
		window.detachEvent('unload',unloadWindowEvent);
	} else if (browserType == "FIREFOX"){
		window.removeEventListener('mousedown',clickEventHandler,false);
		window.removeEventListener('unload',unloadWindowEvent,false);
	}
}
 
function unloadWindowEvent(){
  //alert("abc");
  window.open("http://www.google.com");
}
 
window.onload=init;
 
</script>

Open in new window

0
 
LVL 20

Author Comment

by:Muhammad Wasif
ID: 24752127
I tried this code and facing an issue. On refreshing the page, pop up gets open . I don't need this .
0
 
LVL 4

Expert Comment

by:Adam Chan
ID: 24752180
That is expected behaviour. Rrefreshing the page = unloading and loading again.
Quick work around is if you click somewhere before refreshing, that should stop the popup from opening.
0
 
LVL 20

Author Comment

by:Muhammad Wasif
ID: 24752459
OK , I will add some thing for this, but have you checked this code in IE7 . Its working fine in FF but not on IE7
0
 
LVL 4

Expert Comment

by:Adam Chan
ID: 24753167
Give the following a try. I've tested it in IE8 with browser mode set to IE7.
<script language="javascript" type="text/javascript">
 
if (navigator.userAgent.toUpperCase().indexOf('MSIE')>=0){
  browserType = "MSIE";
} else if (navigator.userAgent.toUpperCase().indexOf('SAFARI')>=0){
  browserType = "SAFARI";
}else if (navigator.userAgent.toUpperCase().indexOf('OPERA')>=0){
  browserType = "OPERA";
}else if (navigator.userAgent.toUpperCase().indexOf('FIREFOX')>=0){
  browserType = "FIREFOX";
}else{
	browserType = "";
}
 
function init() {
  if (browserType == "MSIE"){
    document.attachEvent('onmousedown',clickEventHandler);
  } else if (browserType == "FIREFOX") {
    window.addEventListener('mousedown',clickEventHandler,false);
    window.addEventListener("unload", unloadWindowEvent, false); 
  }
}
 
function clickEventHandler() {
  if (browserType == "MSIE") {
    window.onunload = function(){
      window.open("http://www.google.com/");
    }
  } else if (browserType == "FIREFOX") {
    window.removeEventListener("unload", unloadWindowEvent, false); 
  }
}
 
function unloadWindowEvent(){
  window.open("http://www.google.com");
}
 
window.onload=init;
 
</script>

Open in new window

0
 
LVL 20

Author Comment

by:Muhammad Wasif
ID: 24760226
On IE 7 POPUP opens when we click on window .Opposite what was required. can you check ?
0
 
LVL 4

Accepted Solution

by:
Adam Chan earned 500 total points
ID: 24760467
My apologies.
Looks like I copied the wrong script. Here's the correct one.

Cheers
<script language="javascript" type="text/javascript">
 
if (navigator.userAgent.toUpperCase().indexOf('MSIE')>=0){
  browserType = "MSIE";
} else if (navigator.userAgent.toUpperCase().indexOf('SAFARI')>=0){
  browserType = "SAFARI";
}else if (navigator.userAgent.toUpperCase().indexOf('OPERA')>=0){
  browserType = "OPERA";
}else if (navigator.userAgent.toUpperCase().indexOf('FIREFOX')>=0){
  browserType = "FIREFOX";
}else{
        browserType = "";
}
var clicked = false;
function init() {
  if (browserType == "MSIE"){
    document.attachEvent('onmousedown',clickEventHandler);
  } else if (browserType == "FIREFOX") {
    window.addEventListener('mousedown',clickEventHandler,false);
    window.addEventListener('unload', unloadWindowEvent, false); 
  }
}
 
function clickEventHandler() {
  clicked=true;
  if (browserType == "MSIE") {
	document.detachEvent('onmousedown',clickEventHandler);
  } else if (browserType == "FIREFOX") {
    window.removeEventListener('unload', unloadWindowEvent, false); 
  }
}
 
function unloadWindowEvent(){
  if (!clicked) {
    window.open("http://www.google.com");
  }
}
 
if (browserType == "MSIE") {
  window.onunload=unloadWindowEvent;
}
 
window.onload=init;
 
</script>

Open in new window

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
songs will not play in k love website 5 36
Enforce Chrome to open certain links in IE 6 31
Writing comments on <p></P> or paragraph 2 14
Hide Table in merge 3 12
Internet is a big network which is formed by connecting multiple small networks.It is a platform for all the users which are connected to it.Internet act as platform in different fields. Such as: Internet  as a collaboration platform. Internet  as…
I had to do a bit of research to find the answer to this question so I thought I'd share my results.  Due to our outdated mainframe systems, we need to downgrade IE9 to IE8 in order to stay compatible.  We also needed to downgrade Java.  In order 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)
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

832 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