Organizational Research By

Surprising Reserch Topic

javascript before leaving the page


javascript before leaving the page  using -'javascript,jquery'

I want to make a confirmation before user leaving the page.If he says ok then it would redirect to new page or cancel to leave.I tried to make it with onunload

<script type="text/javascript">
function con() {
    var answer = confirm("do you want to check our other products")
    if (answer){

        alert("bye");
    }
    else{
        window.location = "http://www.example.com";
    }
}
</script>
</head>

<body onunload="con();">
<h1 style="text-align:center">main page</h1>
</body>
</html>


but it confirm after page already closed?How to do it properly.It would be even better if someone shows how to do it with jquery?Any help would be greatly appreciated.Thanks in advance
    

asked Sep 22, 2015 by deepak07.s
0 votes
23 views



Related Hot Questions

5 Answers

0 votes
NULL
answered Sep 22, 2015 by deepak
0 votes
NULL
answered Sep 22, 2015 by amit.gupta
0 votes
NULL
answered Sep 22, 2015 by deepak07.s
0 votes
NULL
answered Sep 22, 2015 by mtabakade
0 votes
NULL
answered Sep 22, 2015 by mca.agarwal

...