Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

JS confirm popup, depending on yes/cancel load a new page



8:10 am on Jul 1, 2010 (gmt 0)

5+ Year Member


If a visitor closes/pressing back on my page(my site only has one page) I want to open a popup with a question. Depending on the answer given, I want to open a new web page.

I have limited to no javascript knowledge, but what I wanted to achieve didn't seem to be difficult when I started...
However, after spending a fair few hours searching on Google and trying several "solutions" I came the following code:

<script language="JavaScript" type="text/javascript">
function confirmit() {
var closeit= confirm("The question?");
if (closeit == true) {
<body onbeforeunload="confirmit();">

This works fine in Firefox and IE 6. In IE 7, 8 and Chrome it shows the popup with the question but nothing happens if you press Yes.

There must be a way to achieve this without using to much complicated javascript code (sticking to the default confirm should do the trick)?


1:24 pm on Jul 1, 2010 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member

I would add in an alert(closeit); to debug what those other browsers are returning. :)


1:53 pm on Jul 1, 2010 (gmt 0)

5+ Year Member


thanks for the reply.

Being a complete JS simpleton... Where do I add that code?



9:33 pm on Jul 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Just after
var closeit= confirm("The question?");
So the alert shows you what the value of closeit is.

However that may not help you it does seem to be correctly coded, but what may be happening is that Chrome is blocking the window.open from firing due to it treating it as a pop-up that is not user initiated (which most browser try to do these days due to pop-up abuse)
I take it that what you are trying to open up a new copy of the page that you are leaving? Because you can't stop the unload event from firing to keep you on the page.

Edit: explanation of what may be happening.


5:15 pm on Jul 4, 2010 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Right . . . . open opens a new window/tab. Instead of this . . . .


try this.

document.location = "http://www.example.com";


7:40 am on Jul 5, 2010 (gmt 0)

5+ Year Member

Thanks for the suggestions!

I added:
just after the
if (closeit == true) {
and I get a popup saying "true" so it does indeed runs the if statement.

document.location = "http://www.example.com";
doesn't work in either in IE7/8 or Chrome. It's not to bad that it doesn't work in Chrome, it is however critical that the script works for IE7 and IE8.

Featured Threads

Hot Threads This Week

Hot Threads This Month