跟踪像素可以从远程服务器执行 JavaScript 吗?

发布于 2024-12-23 16:25:48 字数 657 浏览 0 评论 0原文

我想将跟踪像素放在第 3 方 html 文件上,该文件将调用返回 javascript 的 servlet。该 javascript 应该从本地存储获取一个值并将其报告给其他 servlet。例如:

跟踪像素:

<img src="http://myserver.com/report" width="1" height="1">

报告 servlet 将返回以下 javascript:

var value= localStorage.getItem("myLocalStorageKey");
var serverUrl = "www.myserver.com/myservlet?value=" + value;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", serverUrl, true);
xmlHttp.send("");

这不起作用,因为 src 属性预计不会处理 javascript。 我有什么办法可以做到这一点吗? 如果从第 3 方 html 页面调用 javascript,它将无法从本地存储获取该值,因为该值是从“myserver.com”域输入的,而不是从第 3 方域输入的。

有什么想法吗???

谢谢, 达尼

I want put tracking pixel on 3rd party html file, which will call a servlet that will return javascript. This javascript should get a value from the local storage and report it to other servlet. For example:

Tracking Pixel:

<img src="http://myserver.com/report" width="1" height="1">

The report servlet will return the following javascript:

var value= localStorage.getItem("myLocalStorageKey");
var serverUrl = "www.myserver.com/myservlet?value=" + value;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", serverUrl, true);
xmlHttp.send("");

This doesn't work because the src property is not expect to handle javascript.
Is there any way I can accomplish this?
If the javascript will be called from the 3rd party html page, it won't be able to get the value from the local storage, because the value was entered from "myserver.com" domain, and not from the 3rd party domain.

Any idea???

Thanks,
Dani

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2024-12-30 16:25:48

这不会起作用,期间。 Javascrsipt 对 xmlhttprequests 有一个“同源”安全策略。该脚本的来源将是您的服务器,因此它无法联系除您的服务器之外的任何其他服务器。

您可以使用 JSONP 来解决这个问题,它会动态构建/插入完整的 标记/代码集到页面的 DOM 中,但随后您仍然坚持这样一个事实:您是通过 标记加载此内容,无论如何它都不会执行代码。

相反,请考虑执行类似的操作

<script type="text/javascript" src="http://yourserver/track_me.js"></script>

并使用该脚本提供 jsonp 创建代码。

It won't work, period. Javascrsipt has a 'same origin' security policy for xmlhttprequests. The source of this script would be YOUR server, so it cannot contact any other server EXCEPT yours.

You can get around it somewhat using JSONP instead, which dynamically builds/inserts a full <script>...</script> tag/code set into the page's DOM, but then you're still stuck with the fact that you're loading this via an <img> tag, which won't execute the code anyways.

Instead, consider doing something like

<script type="text/javascript" src="http://yourserver/track_me.js"></script>

and serve up the jsonp-creating code with that script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文