1

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.

6
  • How do you want to send it? AJAX? Commented Dec 19, 2012 at 15:30
  • 1
    Systems like Google Analytics work by GETing something from the logging server (like a 1x1 GIF) with additional data passed in query parameters. The server logs the GET requests. Commented Dec 19, 2012 at 15:30
  • 2
    jQuery is using plain Javascript... let me rephrase that: jQuery is plain Javascript Commented Dec 19, 2012 at 15:34
  • 1
    Yes, jQuery is plain JS, but it is a full-featured library. I would like to avoid loading a ton of unnecessary code. Also, this somewhat of a learning exercise. Commented Dec 19, 2012 at 15:47
  • @11684 Don't have a preference in how to send it exactly. Whatever is easier. Commented Dec 19, 2012 at 15:57

1 Answer 1

2

The simplest way to submit data is to use a form. You can create a form, set action, method attribute, add inputs with data, set it value and then do form.submit(). In order to don't redirect user to your site you should also fill a target attribute 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 Image object in JS and set src field 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:

  • in case of GET requests you can send information and receive it.
  • in case of POST request there is no (as for me) way to get the response.

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).

Sign up to request clarification or add additional context in comments.

Comments

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.