• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

prompt redirection

in this javascript when i close window a prompt will appear.
i want to throw a popup on click of ok, cancel should close the window. pl suggest.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
window.onbeforeunload = function (evt) {
  var message = 'Are you sure you want to leave?';
  if (typeof evt == 'undefined') {
    evt = window.event;
  if (evt) {
    evt.returnValue = message;
  return message;

function pop(url) {
    newwindow = window.open(http://www.yahoo.com, 'name', 'height=500,width=200');
    if (window.focus) { newwindow.focus() }
    return false;

Open in new window

  • 2
1 Solution
onbeforeunload is in such a way that it would display the same confirm box with same message. It can't be changed. We can only pass a string (as your are doing by returning message) and that would be displayed between the default message

"Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page."
yingwhoAuthor Commented:
right now for both ok and cancel it does the same. can u add ur code and show me the difference.
Your code is fine. There is a syntax error in the function pop but nothing is wrong about the onbeforeunload. This is triggered when the user clicks a link or a bookmark or types in the URL, basically anything to go to a new page, this message would appear. If the Ok is pressed, the user would go to the new page. If Cancel is pressed the user would stay on the same page.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now