views:

5148

answers:

3

Hi,

If I'm using JQuery or javascript to do a post, how can I make it target an iframe rather than the current page?

jQuery.post( url, [data], [callback], [type] )

That is the format of the jquery post, it doesn't seem as though there is anywhere to specify the target like there is in the tag.

Any ideas?

Thanks,
Matt

+1  A: 

Maybe you are missing the point of an AJAX request - why are you trying to specify the "target" of an asynchronous request? This makes no sense as the whole point of AJAX is that the request from the server gets sent back to the Javascript, free of page reloads or HTML destinations.

If you wanted to load the result of the request onto an iframe, you might do:

$.post('myurl', function(data) {
    $('#myframe').html(data);
}, 'html');
Paolo Bergantino
I don't really care about ajax. All I want is to submit a post to an iframe from outside of it without the user having to click a submit button. I cannot pass them into the iframe using the src because it is not querystring parameters that it wants. The <form> tag allows me to do exactly what I want, however I'm on .net so everything is contained within a form tag and I cannot create a new form tag for myself with the target attribute. For some reason nothing gets called when I use the code you provided. I've tried replacing the #("#myframe").html(data); with alert("success"); with no luck.
Matt
A: 

As said above, you're missing the point of Ajax. If you wanna post something to an iframe, you could do this by a simple form, posted by JavaScript or by the usual way: a submit button.

Julio Greff
+4  A: 

you can do this via a regular form

<form action="targetpage.php" method="post" target="iframename" id="formid">
   <input type="hidden" name="foo" value="bar" />
</form>

You can then use jQuery to submit the form

$("#formit").submit();

Josh

Josh
can't do this with a form, I'm on .net and you can't have a form within a form
Matt
you could use your main form. change the values of action, target etc temporarily using jquery then restore them after.
Josh
ASP.NET MVC allows multiple forms. Also, I think if you dynamically rendered the form with jQuery then submitted that form ASP.NET would not choke.
spoon16