SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Beer Monster -Ox's Avatar
    Join Date
    Apr 2003
    Location
    sowth afreeka
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    window.onbeforeunload issue

    Greetings all,

    I have written a small function for a custom development we're working on, in this development, (CMS) should you be on the edit page, editing any piece of content, I want to send an alert / confirm to the user should they want to navigate away from the page by clicking on another link.

    Problem:
    Even if I click on the submit button, it send the onbeforeunload message to the browser

    below is the functions;

    HTML Code:
    function checkForEditMode() {    
        if ($('edit_mode')) {    
            window.onbeforeunload = function (event) {
              var message = 'If you navagate away from this page now without having saved your changes, they will be lost';
              if (typeof event == 'undefined') {
                event = window.event;
              }
              if (event) {
                event.returnValue = message;
              }
              return message;
            }
        }
    }
    addLoadListener(checkForEditMode);
    Is there a way that I can ask the browser to send the onbeforeunload to every link EXCEPT the actual save button within the page?

    Thanks in advance to everyone
    My postings are a natural product.
    The slight variations in spelling and grammar enhance its
    individual character and beauty and in no way are to be
    considered flaws or defects - http://www.guinnesspig.net

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    onbeforeunload is attached to the window unload event, not any of the elements whose events call the unload.
    You could add a handler to the save button's click (or a form submit) to cancel the onbeforeunload.

  3. #3
    Beer Monster -Ox's Avatar
    Join Date
    Apr 2003
    Location
    sowth afreeka
    Posts
    374
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been trying all sorted to cancel the beforeonunload, but cant seem to get anything to work on that, any ideas as to how I go about it
    My postings are a natural product.
    The slight variations in spelling and grammar enhance its
    individual character and beauty and in no way are to be
    considered flaws or defects - http://www.guinnesspig.net

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can try:
    Code:
    var useUnload = true;
    
    function doBeforeUnload() {
      if (useUnload) {
        alert("RU sure?");
      }
    }
    
    window.onbeforeunload = doBeforeUnload;
    and then to cancel the unload event from a form submit:

    <form method="post" action="mypage.php" onsubmit="useUnload=false;return true;">

    or from a link:
    <a href="something" onclick="useUnload=false;return true;">mylink</a>

    Any good?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •