简体   繁体   English

编写将包含在其他网站中的js的正确方法是什么?

[英]what's the proper way to write a js that will be included in other websites?

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). 我需要编写一个脚本,我希望将其包含在不同的网站上(类似于您必须包含在网站页面中的Google Analytics js)。

This script has to send to one of my servlets(i'm using java) a request. 这个脚本必须发送给我的一个servlet(我正在使用java)一个请求。

In the servlet i need to increment some variables and after that return an image to the client. 在servlet中,我需要增加一些变量,然后将图像返回给客户端。 The image will be displayed on the website the user accessed. 图像将显示在用户访问的网站上。

I also need to get the client's information (ip,etc) in the servlet. 我还需要在servlet中获取客户端的信息(ip等)。 if i use getRemoteAddr() method, will it work in this case? 如果我使用getRemoteAddr()方法,它会在这种情况下工作吗?

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 is the proper way of doing this ? Whics是这样做的正确方法吗?

Your JS has to print/append an <img> pointing to a 1x1 transparent GIF image to the document. 您的JS必须打印/附加<img>指向文档的1x1透明GIF图像。 All information collected by JS can be sent as a query string on the image URL. JS收集的所有信息都可以作为图像URL上的查询字符串发送。 Google Analytics does similar thing . Google Analytics也做类似的事情

Basically: 基本上:

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

with: 有:

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 + '" />');

Then, in yourdomain.com, you have to map a servlet on the image URL: 然后,在yourdomain.com中,您必须在图像URL上映射servlet

<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>

In the doGet() method of the servlet you can gather all information and finally return a real 1x1 gif image: 在servlet的doGet()方法中,您可以收集所有信息,最后返回一个真正的1x1 gif图像:

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);
}

Let's say you're doing this with PHP: 假设您使用PHP执行此操作:

File: api.js.php 文件: api.js.php

<?php

    // get user ip and do something with it
    $ip = $_SERVER['REMOTE_ADDR'];

    // since we're in a PHP file, we need to tell the client it's actually JS
    header('Content-Type: application/javascript',true);

?>

// your regular JS folows here
alert('Hi there');

you can just put an image tag like this 你可以放一个像这样的图像标签

<img href="yourserver.com/the-servlet-path" />

and serve the image from that request then you dont need to distribute a script 并根据该请求提供图像,然后您不需要分发脚本

the information about the requesting user is all in the servlet api 有关请求用户的信息都在servlet api中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM