8

I could just create a form and use that to do a POST request to any site, thing is the FORM method isn't asynchronous, I need to know when the page has finished loading. I tried messing around with this using an iframe with a form inside, but no success.

Any ideas?

EDIT

unfortunately I have no control over the response data, it varies from XML, json to simple text.

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
  • Why did your iframe endeavours fail? I have a hunch that it should work. Can you give the details of the approach you took? – Ates Goral Jan 21 '09 at 21:23
  • If you have no control over the response data then wouldn't it be a security risk, because the page loaded on the iframe can access your page's DOM using `window.parent`. – andho Oct 25 '10 at 08:26

5 Answers5

12

You can capture the onload event of an iframe. Target your form to the iframe and listen for the onload. You will not be able to access the contents of the iframe though, just the event.

Try something like this:

<iframe id='RS' name='RS' src='about:blank' onload='loaded()'></iframe>

<form action='wherever.php' target='RS' method='POST'>...</form>

script block:

var loadComplete = 0
function loaded() {
    //avoid first onload
    if(loadComplete==0) {
        loadComplete=1
        return()
    }
    alert("form has loaded")
}
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
  • Yeah that be great: I know the data that I want to use has loaded now, great. But you still can't use it. – Pim Jager Jan 21 '09 at 21:25
  • 1
    Awesome, can you show an example? I dont need the data, i just need to know when the event has been fired. – Luca Matteis Jan 21 '09 at 21:27
  • I don't see anywhere in the question where the response data is required. – Diodeus - James MacFarlane Jan 21 '09 at 21:30
  • @Diodus, Ok sorry, I must have read over the OP stating that (since he does)) because there also is something in the OP about content types. – Pim Jager Jan 21 '09 at 21:32
  • Thanks Diodeus that works great apart from one little thing. The "onload" event fires 2 times, as soon as the iframe is loaded (even before submitting the form) and when you submit the iframe. Is there a way to not fire the first load? – Luca Matteis Jan 21 '09 at 21:42
  • I wanted to point out that this method only works in Firefox :( – Luca Matteis Jan 22 '09 at 15:53
  • It should also work on webkit browsers (I checked on Google Chrome). This is similar to the method used in google maps. – andho Oct 25 '10 at 08:12
  • You can also read the contents of the iframe and maybe determine the datatype if needed, but I don't think it is possible to read the response headers. – andho Oct 25 '10 at 08:20
  • This worked only after I removed the () after "return". – Stephen Saucier Jan 29 '14 at 02:00
2

IF you want to make cross domain requests you should either made a JSON call or use a serverside proxy. A serverside proxy is easy to set up, not sure why people avoid it so much. Set up rules in it so people can not use the proxy to request other things.

epascarello
  • 204,599
  • 20
  • 195
  • 236
1

If the data returned from the cross domain post is JSON, then you can dynamically add a script tag pointing to the URI that returns the data. The browser will load that "script" which then you can access from other javascript.

Curtis Shipley
  • 7,990
  • 1
  • 19
  • 28
0

YUI3's IO object offers cross-domain requests, however it does so using a small Flash control it embeds on the page.

While there is work going into secure cross-domain requests from JavaScript, at this time, you need to use a plugin like Flash or Silverlight as a bridge with which to make the request.

foxxtrot
  • 11,214
  • 4
  • 27
  • 27
-7

You can't do anything cross-domain using javascript. You'd have to use a backend language like PHP or asp or something.

Pim Jager
  • 31,965
  • 17
  • 72
  • 98
  • 1
    Baloney. You can add a script tag to the DOM and the SRC of that script can point anywhere. Script tags do not enforce the same domain policy. – Diodeus - James MacFarlane Jan 21 '09 at 21:18
  • No but the OP states that he needs to do something with the data since it can be anything (plain text, JSON, XML). A script tag won't cover that for you. – Pim Jager Jan 21 '09 at 21:22
  • @Diodeus: Also I said that he couldn't do anything cross-domain using javascript, what you are describing is DOM. – Pim Jager Jan 21 '09 at 21:24
  • @Diodeus: You can't POST a Script tag. – Jonathan Lonowski Jan 21 '09 at 21:25
  • Yeah, I know that, I was responding to the assertions of this answer, not the question. – Diodeus - James MacFarlane Jan 21 '09 at 21:28
  • @Diodeus: Ahh! Sorry. ;) Overlooked the "anything" in the answer. Not a good word to use, Pim. ;) – Jonathan Lonowski Jan 21 '09 at 21:30
  • Is it an excuse that I'm no native English speaker? I'm afraid it isn't. On-topic: I really think that I might be the native speaker thing, I don't really see what is wrong with anything there? Javascript engines have SOP so cross-domain stuff can't be done using javascript? – Pim Jager Jan 21 '09 at 21:36
  • 1
    @Pim. Your statment that you "can't do _anything_ cross-domain" is incorrect. GET requests (be they XmlHttp or Script, Img srcs) are acceptable. Its when a client attempts to POST data to a server that cross-domain becomes an issue. – AnthonyWJones Jan 21 '09 at 21:44
  • 1
    You can post anywhere you want. Just set the action attribute of the form element and you can post to any site you care about. However the resulting data will replace whatever document is loaded. If it's in an iframe the parent frame won't be able to see the response because of SOP. – Mr. Shiny and New 安宇 Jan 21 '09 at 21:52