0

I'm using a tracking site to get some statistics. They say I should use the code:

var trackingstring = new String("<script src=\"http://trackingsite.com/track?C=12345&source=js\" type=\"text/javascript\"></script>");
document.write(trackingstring);

However I want to trigger a particular site statistic on a JQuery event rather than on page load, so I can't use document.write.

I believe there's actually no script to run at the URL, so I tried:

var thetrackingURL = "http://trackingsite.com/track?C=12345&source=js";
$.get(thetrackingURL);

However that gives me the error:

MLHttpRequest cannot load http://trackingsite.com/track?C=12345&source=js. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access. 

How can I trigger that URL to be loaded by a JQuery event?

asked Sep 3, 2014 at 23:27
3
  • should be fine if run from a web server... Commented Sep 3, 2014 at 23:35
  • @Holybreath, I got that error testing from a web server (localhost). Do you mean you think it would work if I published it to the live site? Commented Sep 3, 2014 at 23:36
  • That's the problem, most of the time, you are not allowed to issue httprequest against resources, without providing proper headers, it's a security measure. host your page using nginx or something, and see what happens :) Commented Sep 3, 2014 at 23:45

2 Answers 2

1

Try

var script = document.createElement("script");
script.src = "http://trackingsite.com/track?C=12345&source=js";
document.body.appendChild(script);

or, utilizing jquery

$.ajaxSetup({context:document.body});
$.getScript("http://trackingsite.com/track?C=12345&source=js");
Sign up to request clarification or add additional context in comments.

Comments

0

Please read. https://security.stackexchange.com/questions/43639/why-is-the-access-control-allow-origin-header-necessary

Host your page on some local web-server using NGINX or anything else suitable.

I'm sure if you search even stackoverflow, you will find lot's of information regarding this topic.

answered Sep 3, 2014 at 23:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.