0

I need a javascript function where it opens a popup whenever a user closes the window, then in the actual window i need to be able to close that original page that the user tried to close(if the user clicks "I'm sure i want to close" or something)

function openWin()
{
function linkClick(e) {
  alert = false;
}
links = document.getElementsByTagName('a');
for (i = 0; i < links.length; i++)
  links[i].addEventListener('click', linkClick, false);
  if(alert != false){
myWindow=window.open("","","width=200,height=100");
myWindow.document.write("<p>This is 'myWindow'</p>");
}
}

then in the body i have a on unload function

<body onUnload="openWin()" >

the popup works but it appears when you click a link too
any ideas?

mmp51d
  • 41
  • 1

1 Answers1

0

This is untested code, but it should work:

<!-- end of site or in onReady -->
<script>
var lnk = false;

links = document.getElementsByTagName('a');
for (i = 0; i < links.length; i++)
  links[i].addEventListener('click', linkClick, false);
}

function linkClick() { lnk = true; }
function openWin()
{
  if(!lnk){
    myWindow=window.open("","","width=200,height=100");
    myWindow.document.write("<p>This is 'myWindow'</p>");
  }
}
</script>

The diffrence is that the variable lnk and the function linkClick are now in the global scope. onclick should fire before onunload as the onclick-event is usually the one to cancel.

Sebb
  • 871
  • 6
  • 16