最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to handle every link click on a page in JS or jQuery? - Stack Overflow

programmeradmin1浏览0评论

People create their own websites using WYSIWYG creator i give them. These websites have links inside of them. Also they can explore the HTML of the website and put there their own links.

I would like now to handle every link click occurring in website created with my creator and log it to my server. I know how to pass the data from JS or jQuery to PHP server. But what i need to know is how to handle the moment when person clicks a link, postpone the redirection for some moment, and in this time get the url and title of this link and send to my PHP server.

So how to handle every link click (or location change) on website that structure i don't know and get the link and title of the link clicked?

People create their own websites using WYSIWYG creator i give them. These websites have links inside of them. Also they can explore the HTML of the website and put there their own links.

I would like now to handle every link click occurring in website created with my creator and log it to my server. I know how to pass the data from JS or jQuery to PHP server. But what i need to know is how to handle the moment when person clicks a link, postpone the redirection for some moment, and in this time get the url and title of this link and send to my PHP server.

So how to handle every link click (or location change) on website that structure i don't know and get the link and title of the link clicked?

Share Improve this question asked Mar 9, 2012 at 16:04 Tom SmykowskiTom Smykowski 26.2k57 gold badges165 silver badges247 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4
$('a').click(function(e) {
    e.preventDefault();
    var href = $(this).attr('href');
    var title = $(this).attr('title');

    // Send your data to php
    if ($(this).attr('target') === '_blank') {
        window.location.href = href;  // redirect to href
    } else {
        window.open(href);
    }
});
jQuery("a").click(function(){  
  var href = $(this).attr('href');
  $.post('/', {link: href}).then(function(){ document.location = href; });
  return false;
});

just a try

To intercept every link, just place this function somewhere that every page has access to (header/footer/etc.):

$('a').click(function(event) {
    event.preventDefault();//To prevent following the link

    //Your logic. attr('href') and attr('title')
});

You can use jQuery to do this. Use the on event and bind to the click event of a element. You can then do an event.preventDefault(); do your logic and then continue as normal by getting the href from the target.

Why you want to wait till logging is pleted for the redirection. Let it be an asynchronous call so that user don't need to wait. If you want to have your server page in a different domain, to tackle the cross domain ajax issue, use jsonp datatype.

$('a').click(function() {

   $.ajax({
     url:"yourwebsite/loggingpage.php?data="+$(this).attr("href"),
     dataType: 'jsonp' // Notice! JSONP <-- P (lowercase)         

  });

});

and in loggingpage.php, you can read the request data and log it to your persistent storage or wherever you want.

发布评论

评论列表(0)

  1. 暂无评论