Enter the input text inside the iframe and send

I am trying to glue two web services by passing a value from one to the other, unfortunately there is no API or an explicit way to hack the search query, so I need to set the input value inside the iframe.

Here's the markup for a terrible 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 text searchTerm and then submit the form.

Note. This happens on a mobile phone, so I would prefer a really lightweight solution

+7
javascript html forms iframe
source share
3 answers

Isn't it that simple:

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

Make sure your script runs AFTER the iframe loads. The iframe tag has an onload that can be used to determine when the page loads within the frame.

 <iframe src="formPage.html" onload="loaded()" name="myframe" /> 
+6
source share

just remember that if your iframe source does not come from your server, it is not possible to access its contents using javascript from the page containing the iframe. If you have access to the contents of the iframe coming from another server, you can access all the data from the parent page:

 window.top 

If you do not have access to the iframe page, then you cannot do anything.

+5
source share

Can you access the web service from your server code?

If so, I canceled the iframe and create a proxy server on your server. This will ensure your HTML is clean and easy for your target audience.

+2
source share

All Articles