Tracking visitor with javascript - javascript

I have one master domain : www.domain-A.com
I have some domains that call a javascript file on this principal domain.
For example, www.domain-B.com use the file www.domain-A.com/file.js
In this file.js script, I'm trying to put cookie (that's ok) and send some data to www.domain-A.com like the query string, the referrer and the user agent.
I've use that code in my 'file.js' (found on W3School website) :
//Envoi des données
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var json = xmlhttp.responseText;
}
}
xmlhttp.open("POST","http://www.domaine-A.com/script.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(urlParams + "&" + infParams + "&" + u);
It's working very well on FF and Chrome. But, not on IE because I have this error :
This page is accessing information not under its control
So, I've look a little how analytics send data to Google and i seem's use a GIF or something like that but i don't know how to do it in the good way.
My goal is send data from domain-x.com to domain-a.com with javascript.

What version of GA are you using? If you are on analytics.js (Universal Analtyics) You can use ajax and the measurement protocol to call page views on the other site. The measurement protocol lets you make HTTP requests and send raw data right into to GA.
I have used it before to track interaction with interactive infographic that I had on another site.
For your case you can use the following paramaters
www.google-analytics.com/collect
v=1 // Version.
&tid=UA-XXXX-Y // Tracking ID / Web property / Property ID.
&cid=555 // Anonymous Client ID.
&t=pageview // Pageview hit type.
&dh=mydemo.com // Document hostname.
&dp=/home // Page.
&dt=homepage // Title.

Related

How to send a parameter from java script function to a servlet

I want to send some parameters to a servlet with the help of javascript.....
In my jsp page there is a parameter "a[j]" which is generated dynamically and on a click the javascript function invoke and this a[j] parameter which is "ur" in java script function should be send to a servlet named Rank.....
Tell me which function should i use to forward the parameter...
IN Jsp:
<%out.println(a[j+1]);%>
Javascript:(rank)
<script type="text/javascript">
function rank(ur)
{ ??????????("Rank?set="+ur);
}
</script>
In Servlet(Rank):
String s = (String)request.getParameter("set");
You could do
window.location="Rank?set="+ur;
or
document.body.innerHTML+='<form id="myform" action="Rank" method="get"><input name="set" value="'+ur+'" /></form>';
document.getElementById('myform').submit();
Edit:
Ah, I think that what you need is AJAX, a group of interrelated web development techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page.
Then the code is:
var xmlhttp;
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//only if you want the response
document.getElementById("log").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","Rank?set=" + ur,true);
xmlhttp.send();
}

Path issue in ajax ,in a wordpress site

I have a wordpress site
for ex, www.test.com
There is a js file (forms.js)in the script folder of mytheme folder.
ie (theme/mytheme/script/forms.js)
There is a mail.php page in mytheme folder
ie (theme/mytheme/mail.php)
Following is the content of forms.js
function submitFormToEmail()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("results").innerHTML=xmlhttp.responseText;
alert("Form received, thank you!");
}
}
xmlhttp.open("GET","/mail.php",true);
xmlhttp.send();
}
I will call "submitFormToEmail",if we click a image in the page *www.test.com/hello_test*
Where "hello_test" page lies in theme/mytheme/.
But the mail.php is not working.
Let me know ,how can we set the path of mail.php in the function "submitFormToEmail", so that it will work
Calling /mail.php will look for the mail.php file at http://www.test.com/mail.php. Just fix the path?
Either directly code the correct path into the javascript file or use php to dynamically get it using get_bloginfo. There are several ways to do this, but probably the best way is to create a hidden element on the page that contains the url and then fetch this when building your ajax request. Otherwise you'd have to set php to parse js files and include it in there. Doesn't seem like a good way to me though.
get_bloginfo('template_url');

XMLHttpRequest.open does not work with IE9 (though XMLHttpRequest object is supported) [duplicate]

var xhttp=new XMLHttpRequest();
xhttp.open('GET', 'foo.xml', false);
F12 pops back: SCRIPT5: Access is denied. on Line 95, which is the xhttp.open line.
My JavaScript seems well-formed, and Firefox does what I think it should.
I've read a lot of questions very similar to this one, so I've checked out the Same Origin Policy, but I can't see how it'd apply considering foo.xml is in the same directory as the html file. I opened up the scripting permissions on my local intranet, and told McAfee to take a five-minute break, just to be sure. I even tried running IE as admin, so this can't really be a permissions issue can it? Why else would IE be denied access to a local file?
Maybe you like to check the links below:
Making cross domain JavaScript requests using XMLHttpRequest or XDomainRequest
XMLHttpRequest – Mozilla Developer Network
A good summary of the jQuery x-domain requests
Which browser supports x-domain?
You likely have a Mark-of-the-Web on the local file. See http://blogs.msdn.com/b/ieinternals/archive/2011/03/23/understanding-local-machine-zone-lockdown-restricted-this-webpage-from-running-scripts-or-activex-controls.aspx for an explanation.
This example illustrate how to use AJAX to pull resourcess from any website. it works across browsers. i have tested it on IE8-IE10, safari, chrome, firefox, opera.
if (window.XDomainRequest) xmlhttp = new XDomainRequest();
else if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", "http://api.hostip.info/get_html.php", false);
xmlhttp.send();
hostipInfo = xmlhttp.responseText.split("\n");
var IP = false;
for (i = 0; hostipInfo.length >= i; i++) {
if (hostipInfo[i]) {
ipAddress = hostipInfo[i].split(":");
if (ipAddress[0] == "IP") {
IP = ipAddress[1];
}
}
}
return IP;
On IE7, IE8, and IE9 just go to Settings->Internet Options->Security->Custom Level and change security settings under "Miscellaneous" set "Access data sources across domains" to Enable.
This error message (SCRIPT5: Access is denied.) can also be encountered if the target page of a .replace method is not found (I had entered the page name incorrectly). I know because it just happened to me, which is why I went searching for some more information about the meaning of the error message.
Most likely, you need to have the Javascript served over SSL.
Source: https://www.parse.com/questions/internet-explorer-and-the-javascript-sdk
I think that the issue is that the file is on your local computer, and IE is denying access because if it let scripts have access to files on the comp that the browser is running on, that would be a HUGE security hole.
If you have access to a server or another comp that you could use as one, maybe you could try putting the files on the that, and then running the scripts as you would from a website.
Probably you are requesting for an external resource, this case IE needs the XDomain object. See the sample code below for how to make ajax request for all browsers with cross domains:
Tork.post = function (url,data,callBack,callBackParameter){
if (url.indexOf("?")>0){
data = url.substring(url.indexOf("?")+1)+"&"+ data;
url = url.substring(0,url.indexOf("?"));
}
data += "&randomNumberG=" + Math.random() + (Tork.debug?"&debug=1":"");
var xmlhttp;
if (window.XDomainRequest)
{
xmlhttp=new XDomainRequest();
xmlhttp.onload = function(){callBack(xmlhttp.responseText)};
}
else if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200){
Tork.msg("Response:"+xmlhttp.responseText);
callBack(xmlhttp.responseText,callBackParameter);
Tork.showLoadingScreen(false);
}
}
xmlhttp.open("POST",Tork.baseURL+url,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(data);
}
I had faced similar issue on IE10. I had a workaround by using the jQuery ajax request to retrieve data:
$.ajax({
url: YOUR_XML_FILE
aync: false,
success: function (data) {
// Store data into a variable
},
dataType: YOUR_DATA_TYPE,
complete: ON_COMPLETE_FUNCTION_CALL
});
$.ajax({
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
alert(location.ip);
}
});
This code will work https sites too
Open the Internet Explorer Developer Tool,
Tools -> F12 developer tools. (I think you can also press F12 to get it)
Change the Document Mode to Standards. (The page should be automatically refresh, if you change the Document Mode)
Problem should be fixed.
Enjoy

how to use xmlhttprequest object's post method for sending data

I want to send data that is in a java script variable to the server.the variable is in a method that is executing when I click a button on the web site.here is the code written in that method for sending data.
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","new_map.php",true);
xmlhttp.send(cur_entry_string);
and here is code written in new_map.php file for getting data.here cur_entry_string is the variable that is holding that data.
$massage = $_POST[cur_entry_string];
but this is not working..:(...I am using eclipse.
Maybe you should have a look at an AJAX tutorial http://www.w3schools.com/ajax/default.asp and if that is not what you want to do you can also look into JSON
You need to actually generate a valid query string. A query string in POST looks the same as a GET string.
Something like this should work:
xmlhttp.send('cur_entry_string=' + cur_entry_string);
I would recommend using a library such as jQuery for using Ajax, as it simplifies the process a lot so you don't need to do error prone things like these query string things yourself.
ps. note that you should enclose array string indices in quotes when using PHP:
$_POST['cur_entry_string']

How to replace IP addresses represented in the page as text with its IP location (as "Country, Region, City" for example)

I'm trying to write Opera user JS for replacing IP addresses represented in the page as text with its IP location. After googling for a while I found a service which provides exactly the information I needed.
This service for a HTTP query returns the header of the page with meta containing the needed info.
As example, this query: http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=142.34.25.111
(see source of the page)
In the browser this request works fine.
I tried to get this text (as XML) via AJAX, but encountered an error ReferenceError: Security violation at line xmlhttp.send();.
So, my questions is:
- Is existing any practice to obtain IP location from IP address via JS (without any fee || registration)
- How I can to replace some content of the one page to modified content of the another page via JS?
PS. I am very new in JS programming.
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
var x = xmlhttp.responseXML.documentElement.documentElement;
var i, s, xx;
s = '';
for (i=0; i < x.length; i++)
{
xx = x[i].documentElement;
if (xx.attributes[0] == 'country') s = s + xx.attributes[1].textContent;
if (xx.attributes[0] == 'region') s = s + ', ' + xx.attributes[1].textContent;
if (xx.attributes[0] == 'city') s = s + ', ' + xx.attributes[1].textContent;
}
document.getElementById("myDiv").innerHTML = s;
}
}
xmlhttp.open("GET","http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress=142.34.25.111",false);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
Test page included.
Ok, Thanks a lot to all!
May be my question is not clear enough.
Lets try again (I told this to myself):
I need JS function like IPAddress2IPLocation(InIPAddress) which returns IP location in format Country, Region, City using passed parameter.
Is anybody know answer for my question?
At this time (3:50 AM here) I approved for any suggestions :)
But, in any case, it is not extremely bout my life - it is just interesting about JS.
Javascript security! Fear not, there are ways to work around the same origin policy.
Getting around same origin policy in javascript without server side scripts
ReferenceError: Security violation is not asking you to pay a fee. You are doing Cross-site Scripting which is a security violation on most browsers.
You can avoid this error using a proxy server.
This is probably due to accessing another domain from Ajax. For security reasons, only requests to the same domain name can be made.
The code http://www.codeproject.com/KB/aspnet/aspxcode_net.aspx describes the basics for building ip to location translation.

Categories

Resources