homepage Welcome to WebmasterWorld Guest from 54.205.99.71
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

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




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

Hi,

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:

<html>
<head>
<script language="JavaScript" type="text/javascript">
function confirmit() {
var closeit= confirm("The question?");
if (closeit == true) {
window.open("http://www.thenewpagethatopens.com");
}
}
</script>
</head>
<body onbeforeunload="confirmit();">
</body>
</html>


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)?

 

whoisgregg




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

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

glimbeek




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

Hi,

thanks for the reply.

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

George

Dijkgraaf




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

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.

rocknbil




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

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

window.open("http://www.example.com");

try this.

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

glimbeek




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

Thanks for the suggestions!

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

Using:
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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved