[英]How does a webpage determine if/when something is viewed?
I'm seeking to understand the code and function(s) that determines when a particular part of a webpage has been viewed (for mere understanding). 我正在寻求了解确定何时浏览网页特定部分的代码和功能(仅用于理解)。
For example, on the StackExchange websites, it determines when a person has fully read the "about" page (and awards a badge for such). 例如,在StackExchange网站上,它确定一个人何时完全阅读“关于”页面(并为此授予徽章)。 What function/code/language is watching for this?
正在观看什么功能/代码/语言?
Or at least, how could I trace and/or determine where this function is in a page's source? 或者至少,我如何跟踪和/或确定此功能在页面源中的位置?
You can use JavaScript to detect how far a user has scrolled on the page, and if they've scrolled past a certain point (say 80% of the article) you can fire off an AJAX request which logs that the user has scrolled past that point. 您可以使用JavaScript来检测用户在页面上滚动的距离,如果他们滚动超过某个点(比如文章的80%),您可以触发一个AJAX请求,该请求会记录用户滚动过去的那个点。
Quick example in jQuery: jQuery中的简单示例:
$('body').scroll(function () {
if($(this).scrollTop() > 1000) {
// Fire off AJAX request
}
});
Didn't test that, and you'll want to change 1000
to be the amount of pixels until you get to an element in the article so it works for different article lengths. 没有测试,并且你想要将
1000
改为像素数量,直到你到达文章中的元素,因此它适用于不同的文章长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.