How can I detect when the user is leaving my site, not just going to a different page?

前端 未结 5 2016
攒了一身酷
攒了一身酷 2020-12-29 10:08

I have a handler for onbeforeunload

window.onbeforeunload = unloadMess;
function unloadMess(){
  var conf = confirm(\"Wait! Before you go, please share your          


        
5条回答
  •  感情败类
    2020-12-29 10:49

    I've got one idea, but I don't know if it's work. My suggestion is, add each link an onClick event with a function call. That function reads just the href attribute and store into a variable with global scope.

    var clickedHrefAttrValue = "";
    function getClickUrl(currentLink)
    {
       clickedHrefAttrValue = $(currentLink).attr("href");
       return true;
    }
    

    The html for the a tags must be looks like following:

    Linktext
    

    and in your given function:

    function getClickUrl()
    {
       if (clickedHrefAttrValue.indexOf("" > -1)
       {
         //what ever you want do to
       }
    }
    

    It is just an idea, but I think it is worth to try it.

提交回复
热议问题