什么是编写将包含在其他网站一个js的正确方法? [英] what's the proper way to write a js that will be included in other websites?

查看:119
本文介绍了什么是编写将包含在其他网站一个js的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写,我想包括在不同的网站上(类似的东西与你在你的网页,包括谷歌Analytics的js)的脚本。

I need to write a script which i want to include on different websites(something similiar with the google analytics js that you have to include in your website pages).

该脚本有发送给我的servlet一(我使用的Java)的请求。

This script has to send to one of my servlets(i'm using java) a request.

在我需要递增一些变量和之后的图像返回给客户端小服务程序。图像将显示在用户访问的网站上。

In the servlet i need to increment some variables and after that return an image to the client. The image will be displayed on the website the user accessed.

我还需要获得该客户的信息(IP等)在servlet。如果我使用的getRemoteAddr()方法,将它在这种情况下工作?

I also need to get the client's information (ip,etc) in the servlet. if i use getRemoteAddr() method, will it work in this case?

此外,我需要跟踪我上显示的客户端的图像。(我不知道这应该是,客户端或服务器端)。

Furthermore i need to keep track on the images i displayed to the client.(i don't know where this should be, client side or server side).

Whics是这样做的正确方法?

Whics is the proper way of doing this ?

推荐答案

您的JS还具有打印/追加&LT; IMG&GT; 指向一个1x1的透明GIF图像该文档。通过JS收集的所有信息可以被作为对图像的URL查询字符串。谷歌Analytics(分析)确实<一href="http://$c$c.google.com/intl/en-US/apis/analytics/docs/tracking/gaTrackingTroubleshooting.html#gifParameters"相对=nofollow>类似的事情。

Your JS has to print/append an <img> pointing to a 1x1 transparent GIF image to the document. All information collected by JS can be sent as a query string on the image URL. Google Analytics does similar thing.

基本上是:

<script src="http://yourdomain.com/track.js"></script>

var requestURI = window.location;
var referrer = document.referrer;
var resolution = screen.width + 'x' + screen.height;
var colorDepth = screen.colorDepth;
// ...

var query = '?requestURI=' + encodeURIComponent(requestURI)
          + '&referrer=' + encodeURIComponent(referrer)
          + '&resolution=' + encodeURIComponent(resolution)
          + '&colorDepth=' + encodeURIComponent(colorDepth);

document.write('<img src="http://yourdomain.com/track.gif' + query + '" />');

然后,在yourdomain.com,你必须映射的servlet 上的图像的URL:

Then, in yourdomain.com, you have to map a servlet on the image URL:

<servlet>
    <servlet-name>trackServlet</servlet-name>
    <servlet-class>com.example.TrackServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>trackServlet</servlet-name>
    <url-pattern>/track.gif</url-pattern>
</servlet-mapping>

在Servlet的的doGet()方法,你可以收集所有信息,并最终返回一个真正的1x1的GIF图像:

In the doGet() method of the servlet you can gather all information and finally return a real 1x1 gif image:

private static final byte[] GIF = {
    71, 73, 70, 56, 57, 97, 1, 0, 1, 0, -16, 0, 0, 0, 0, 0, 0, 0, 0, 33, -7,
    4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 68, 1, 0, 59
};

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Gather JS-collected parameters.
    String requestURI = request.getParameter("requestURI");
    String referrer = request.getParameter("referrer");
    String resolution = request.getParameter("resolution");
    String colorDepth = request.getParameter("colorDepth");
    // ...

    // Gather server-collected parameters.
    String remoteAddr = request.getRemoteAddr();
    String userAgent = request.getHeader("user-agent");
    // ...

    // Send 1x1 transparent gif (and disable browser caching on it!)
    response.setHeader("Content-Type", "image/gif");
    response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.getOutputStream().write(GIF);
}

这篇关于什么是编写将包含在其他网站一个js的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆