I have a JS script that is hosted on domain X, but is embedded on domain Y. I would like to send some data back to a page on domain X (via POST probably).
I tried searching for a solution, but most information I found involves using jQuery. Is it possible to do this via plain JavaScript? Should I be worried about cross-site scripting issues?
Basically, I am trying to have something similar to Google Analytics (a little widget sending data back home). I can process the data, but my JavaScript experience is very limited.
The simplest way to submit data is to use a
form. You can create a form, setaction,methodattribute, addinputs with data, set it value and then doform.submit(). In order to don’t redirect user to your site you should also fill atargetattribute with name of a hidden iframe. In that case form will be sumbitted “in it”. Page woun’t reload, but your page will be loaded into iframe (which is hidden). This method allows you to do POST requests.If you need only GET request, things are even simplier: create an
Imageobject in JS and setsrcfield with the requested url. I don’t know how crossbrowser and stable this solution is, but as far as I remember, it works. In fact, you can create and add to page any other element that loads it’s content (style, script (and put some codde in response to be evaluated) ).About crossdomain stuff:
I suggest you to read something about DOM manipulations in JavaScript and HTML forms to understand it’s mechanics and what to do in order to simulate form submission in JS (basically I described it in first two paragraphs).