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.

2
  • Why did your iframe endeavours fail? I have a hunch that it should work. Can you give the details of the approach you took? Commented Jan 21, 2009 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. Commented Oct 25, 2010 at 8:26

5 Answers 5

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")
}
Sign up to request clarification or add additional context in comments.

9 Comments

Yeah that be great: I know the data that I want to use has loaded now, great. But you still can't use it.
Awesome, can you show an example? I dont need the data, i just need to know when the event has been fired.
I don't see anywhere in the question where the response data is required.
@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.
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?
|
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.

Comments

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.

3 Comments

I have no control over the response data.
Also script tag is only GET, -1 for that.
Certainly the response-type isn't arbitrary. They won't send you XML once, and then the next request will be JSON. There's got to be some logic behind what determines the return-types.
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.

1 Comment

You are ignoring the fact that iframe method can be used to make this request.
-7

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

9 Comments

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.
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.
@Diodeus: Also I said that he couldn't do anything cross-domain using javascript, what you are describing is DOM.
@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.
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.
|

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.