I am trying to write some javascript which asks a user, when they leave the page, if they want to fill out a survey (however annoying this may be!). I thought my solution was found by an answer on this site. This is the code I currently have that does not seem to be working:
<script language="javascript">
function comfirmsurv() {
var ConfirmStatus = confirm("We would love to receive your feedback on your experience of this page. Would you like to complete our short survey?");
if (ConfirmStatus == true) {
window.open("#");
}
else
{window.close();}
}
}
window.onUnload=confirmsurv()
</script>
<body>
Test
</body>
Any help is greatly appreciated.
You are assigning the result of calling the function, not the function itself:
window.onunload = confirmsurv; // Note no ()
A few other issues:
window.close()
: you can only close windows you open, not others.}
.type="text/javascript"