开发者

Can I use javascript/jquery to submit a non-local form in an iframe?

I am building a page at http://www.localurl.com.

I want to submit a POST form that is on http://www.url.com/form_step_1.html to http://www.url.com/form_step_2.html and display http://www.url.com/form_step_2.html开发者_StackOverflow中文版 in an iframe.

Is this possible to do with JavaScript/jQuery or does browser security prohibit this? Is there anyway it can be done with ajax?

In addition to the selected answer, I found this blog post extraordinarily informative on the subject.


You cannot execute Javascript on a different domain that you do not have access to.

If you do have access then use:

document.domain = 'example.com'

on both pages. You can then submit a form within the iFrame with this:

window.top.myiframename.document.myformname.submit();

or

window.myiframename.document.myformname.submit();

NOTE: This probably won't work in IE8.

Alternatively, you could create your own page on your domain using php:

<?php
    echo file_get_contents("http://www.url.com");
?>

Open this page (which is on your local host) in your iframe and use:

    window.top.myiframename.document.myformname.submit();

or window.myiframename.document.myformname.submit();


This isn't exactly what you want, but you can submit a form with its target set to the name of the iframe.

On foo.com:

<form action="http://bar.com/action" method="POST" target="baz">
  ...
</form>

<iframe name="baz" ...></iframe>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜