views:

1054

answers:

3

I'm trying to glue together two web services by passing a value from one to the other, unfortunately there's no API or clear way of hacking up the search query so I need to set the value of a input inside an iframe.

Here's the markup for the horrible iframe.

<form id="searchForm" method="post" action="/search/initialSearch">         
  <fieldset class="searchFields">
    <input type="text" name="searchTerm" value=""/>
    <input type="submit" value="Find stops"/>
  </fieldset>
</form>

I need to set the searchTerm text and then submit the form.

Note: This is going over mobile, so I would prefer a really lightweight solution

+1  A: 

Isn't it as simple as:

myframe.document.getElementById("searchForm").searchTerm.value = 'hello';
myframe.document.getElementById("searchForm").submit();

Make sure your script runs AFTER the iframe is loaded. Your iframe tag has an onload event that you can use to determine when the page within the frame is loaded.

<iframe src="formPage.html" onload="loaded()" name="myframe" />
Jonathan Sampson
Thanks, how can I make sure that the frame is loaded? is there some kind of event that is triggered?
Tom
You can add a function reference to the iframe's onload attribute: <iframe onload="imLoaded()" ... />
Jonathan Sampson
You'll have issues if you're doing stuff cross-domains, that's one thing to watch out for. Otherwise, that's definitely a workable solution.
Stobor
Absolutely right, Stobor. I went ahead and updated the code - I instinctively used google as my example - shouldn't have :)
Jonathan Sampson
Thanks for the updated code sample, very clean. I actually doing cross domain stuff, so it looks like I need a little proxy
Tom
Yeah; you'll need a proxy if you're going cross-domain.
Jonathan Sampson
+1  A: 

Are you able to access the web service from your server code?

If so, I'd sack off the iframe and create a proxy on your server. This would keep your HTML clean and lightweight for your intended audience.

kim3er
That seems like a good idea, I'm using php got any code or links for a simple proxy?
Tom
Sorry Tom I'm an ASP.NET man myself. If I come across any examples I'll post them.
kim3er
A: 

just be aware that if your iframe's source is not coming from your server, it is impossible to access its contents with javascript from the page that contains the iframe. If you have access to the contents of the iframe that is coming from another server, then you can access all of the data from the parent page with:

window.top

If you do not have access to the iframe's page, then there is nothing you can do.

strife25