prompt redirection

Posted on 2009-12-22
Last Modified: 2012-05-08

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"  "">



<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 =, 'name', 'height=500,width=200');

    if (window.focus) { newwindow.focus() }

    return false;








Open in new window

Question by:yingwho
    LVL 58

    Accepted 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."

    Author Comment

    right now for both ok and cancel it does the same. can u add ur code and show me the difference.
    LVL 58

    Expert Comment

    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

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Skill Development 15 101
    Back Button Causing Issues With Hidden Divs 5 50
    jquery inside Bootstrap modal 8 40
    coldfusion, jQuery help 25 43
    Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question ( on how to make a page show some balloons animate up a page…
    Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now