How can I prevent [removed] from being triggered by [removed] href links in IE?

前端 未结 6 1659
清酒与你
清酒与你 2021-01-30 05:24

I\'m building a fail safe for my form that is going to warn users that if they leave the page their form data will be lost (similar to what gmail does).

window.         


        
相关标签:
6条回答
  • 2021-01-30 05:58

    I just ran into a similar problem and found a very easy solution:

    $("a").mousedown(function() {
        $(window).unbind();
    });
    

    This will remove the onbeforeunload event just before the click event is triggered.

    0 讨论(0)
  • 2021-01-30 06:03

    You may remove and re-assign the onbeforeunload when hovering those links:

    jQuery(
      function($)
      {
          //store onbeforeunload for later use
        $(window).data('beforeunload',window.onbeforeunload);  
    
          //remove||re-assign onbeforeunload on hover 
        $('a[href^="javascript:"]')
          .hover( 
                 function(){window.onbeforeunload=null;},
                 function(){window.onbeforeunload=$(window).data('beforeunload');}
                );
    
      }
    );
    
    0 讨论(0)
  • 2021-01-30 06:03

    Move any script to a click event handler instead, with a fallback page for users without JavaScript:

    <a href="foo.html" onclick="someFunction(); return false">click</a>
    

    The unobtrusive JS champions out there will probably object to the use of the onclick attribute, but the fact is that it works, it's the simplest way to add an event handler and the simplest way to provide an example. For better separation of concerns, you could instead use either a DOM0 onclick property, addEventListener() / attachEvent() or a library.

    0 讨论(0)
  • 2021-01-30 06:13

    (Like some of the other answers, this requires changing how the JavaScript is bound. If that doesn't work for you, disregard.)

    I found that in IE 7 through IE 10, simply using jQuery .click() with preventDefault works without showing the onbeforeunload message (IE 11 does not show the beforeunload message for any of my test cases). This is true whether the href is '#' or javascript:void(0). I expect the conclusion holds if attachEvent/addEventListener is used directly, but I didn't test that.

    There is a demo of the below at http://jsbin.com/tatufehe/1 . Both of the versions (in green) with preventDefault work (don't show the beforeunload dialog). The one without preventDefault does show it (as a comparison).


    $( document ).ready( function () {
      $( 'a' ).click( function ( evt ) {
        $( '#display' ).text( 'You clicked: ' + $( this ).text() );
    
        if ( $(this).hasClass( 'withPreventDefault' ) ) {
          evt.preventDefault();
        }
      } );
    })
    
    window.onbeforeunload = function () {
      return "Are you sure you want to leave?";   
    };
    

    <p id="display"></p>
    
    <p><a class="withPreventDefault" href="#">Number sign link *with* preventDefault</a></p>
    
    <p><a class="withPreventDefault" href="javascript:void(0);">JavaScript href link *with* preventDefault</a></p>
    
    <p><a href="javascript:void(0);">JavaScript href link *without* preventDefault</a></p>
    
    0 讨论(0)
  • 2021-01-30 06:21

    If the user mouse is on a link and they trigger refresh page with the keyboard or activate a link with the keyboard, the prompt won't show up. So Dr.Molle approach will not work in this rare case.

    0 讨论(0)
  • 2021-01-30 06:22

    If you include a bookmark symbol in the href of the a tag you should be able to still use the onclick event to include your JavaScript.

    <a href="#" onclick='someFunction();'>click</a>
    

    I’ve run a number of tests and this appears to execute the JavaScript without triggering the onbeforeunload event. I’d be curious to know if this works for others or if you still have the same problem after implementing a tags in this manner.

    0 讨论(0)
提交回复
热议问题