mailto link (in chrome) is triggering [removed] - can i prevent this?

后端 未结 7 1328
鱼传尺愫
鱼传尺愫 2021-02-18 19:09

Possibly related to How to open mailto link in Chrome with Window.open without creating a new tab?

Hi all. I have a form page where i\'ve put a window.onbeforeunload co

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-18 19:32

    Another option, also a bit hacky but a bit more generic way to make the beforeunload ignore certain types of links:

    In the beforeunload, inspect the link that caused the beforeunload, and if it is not a http or https link, don't bother the user.

    Unfortunately it is not easy to inspect the link that caused the beforeunload, so this is where it gets a little hacky with an onclick handler and global variable.

    var lastClicked = null;
    window.onclick = function(e){
        e = e || window.event;
        lastClicked = e.target
    }
    window.onbeforeunload = function(e){
        if (changed && lastClicked && lastClicked.href 
            && lastClicked.href.substring(0,4) == 'http')
            return "You have unsaved changes";
    }
    

    Edit: Almost forgot, this answer comes from here: https://stackoverflow.com/a/12065766/421243

提交回复
热议问题