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
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
and serve up the jsonp-creating code with that script.