开发者

javascript/php - record click destination before redirect

开发者 https://www.devze.com 2023-03-19 13:00 出处:网络
Say I have the link: <a href=\"http://www.google.com\">google</a> Is there any way I can record the click (with php/sql)? For example an onclick event to load ajax? Would the ajax run b

Say I have the link:

    <a href="http://www.google.com">google</a>

Is there any way I can record the click (with php/sql)? For example an onclick event to load ajax? Would the ajax run before the page redirects? I want to avoid:

    <a href="rec开发者_C百科ord_click.php?url=http://www.google.com">google</a>


You could change your link to:

<a href="test.php" onclick="javascript: recordClick(this); return false;">TEST</a>

And then use the function:

function recordClick(t) {
    //Insert AJAX here. t contains the URL
    document.location = t;
}

You could then wait for the AJAX request to complete before using the document.location part. However I would suggest simply using a separate file to log clicks like you have already suggested because it is an immediate action that doesn't require JS to be on, and that provides immediate feedback.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号