[英]How can I add a simple counter function in javascript jquery?
I'm using Shadowbox a jquery overlay and I wanted to count how many people are actually using the overlay. 我正在使用Shadowbox进行jquery覆盖,我想计算实际上有多少人在使用该覆盖。 Thus, I would need a function that would write a counter to a file or sending a query through a php api...
因此,我需要一个可以将计数器写入文件或通过php API发送查询的函数...
has to be aa php url api because I cant use php on the server where the overlay is. 必须是一个php url api,因为我无法在覆盖层所在的服务器上使用php。
So I need help with executing a javascript function on the overlay click, tips on how to make a counter query through GET method. 因此,我需要在点击重叠广告时执行javascript函数方面的帮助,以及有关如何通过GET方法进行计数器查询的提示。
Thanks 谢谢
<script type="text/javascript">
Shadowbox.init({ handleOversize: "resize", overlayOpacity: 0.9 Shadowbox.init({handleOversize:“ resize”,overlayOpacity:0.9
});
When you bind your click handler to open the shadownbox, add a binding for an ajax call, such as this: 绑定单击处理程序以打开shadownbox时,请为ajax调用添加绑定,例如:
$.ajax({
type: "GET",
url: "stats.js",
data: "name=urlOrNameOfItem"
});
Replace urlOrNameOfItem with something meaningful so you can track what has been clicked. 用有意义的内容替换urlOrNameOfItem,以便您可以跟踪单击的内容。 I assume you know in php how to handle a query string.
我假设您知道在php中如何处理查询字符串。
See JQuery docs: http://api.jquery.com/jQuery.ajax/ 请参阅JQuery文档: http : //api.jquery.com/jQuery.ajax/
Before you display your Shadowbox throw an Ajax query to a php script which would save the current request in a db (including $_SERVER info for better analysis). 在显示Shadowbox之前,向PHP脚本抛出一个Ajax查询,该查询会将当前请求保存在db中(包括$ _SERVER信息以进行更好的分析)。 This PHP script can fetch the current count of views for that image from the Db and update it accordingly.
该PHP脚本可以从Db获取该图像的当前视图计数,并相应地对其进行更新。
I'm guessing the shadowbox function is called as a onclick event on your image so just add the Ajax call something like this: 我猜想shadowbox函数被称为图像上的onclick事件,因此只需添加如下的Ajax调用即可:
$.ajax({
url: 'path-to-counter-script.php?i='+image-identifier,
success: function() {
//display shadowbox
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.