0

Is it possible to create a Javascript file that act as "click counter"?

What I want to do is that I put that Javascript file in my domain

mydomain.com/js/clickcounter.js

And anyone can include it to start counting clicks..

Example for THEIRdomain.com/index.html

 <script src="mydomain.com/js/clickcounter.js"></script> 

And a possible script:

function myDomain_TrackClicks(){
    xhttp.open("POST", "mydomain.com/trackClicks.php", true);
    xhttp.send( "pagevisited=" + encodeURIComponent( window.location.href));
}

if(window.attachEvent) {
    window.attachEvent('onload', myDomain_TrackClicks);
} else {
    if(window.onload) {
        var curronload = window.onload;
        var newonload = function(evt) {
            curronload(evt);
            myDomain_TrackClicks(evt);
        };
        window.onload = newonload;
    } else {
        window.onload = myDomain_TrackClicks;
    }
}

Or is that limited by some origin policy? Origin policy really confuses me.

1
  • Please comment if there are problems with the question Commented Aug 18, 2016 at 9:08

1 Answer 1

1

All thats required is your server must respond with the correct HTTP headers when mydomain.com/js/clickcounter.js is requested by the browser.

Specifically, this header must be included in the response headers:

Access-Control-Allow-Origin: *

This tells the browser that the resource may be requested from any origin.

Recommended reading - https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.