0

I want to execute few functions before the page just exits such as logging page exit time.

The onbeforeunload event when used shows a pop-up which I don't want.

Somehow when an outbound link is clicked, it should enter a function, fetch the link href, execute and then exit the page.

Without using JQuery.

Is this possible?

4

2 に答える 2

1
links = document.getElementsByTagName('a')
for(var i=0; i<links.length; i++){
    links[i].addEventListener('click', function(event){
        //do your stuff
        return true;
    });
};

そのショットを与えます。

リンク自体に関する情報が必要な場合は、this構成を使用してください。具体的には:

this.href

リンクアドレスを取得します。

http://jsfiddle.net/6PEmj/5/

于 2012-09-14T15:54:31.520 に答える
1

onbeforeunload戻り値が指定された場合にのみアラートします。

これを使用して、アウトバウンドリンクがクリックされたことを検出してみてください。

var a = document.getElementsByTagName('a'),
    i = a.length,
    domain = new RegExp(location.href.match(/(^.*)\./)[1]),
    onLeave = function () {
        if (!domain.test(this.href)) {
            alert('Leaving!');
        }
    };

while (i--) {
    a[i].onclick = onLeave;
}
于 2012-09-14T16:15:08.900 に答える