0

I have a web page that contains two textboxes and a button. I can't control this page by adding any thing to it, or changing any thing in it.

I want to (in some way) write some text in the first textbox when the page loads, without pressing the button.

I want to put the page in a frame and then control it from the container page

I know that I have to use jquery but I need a sample that explain that or to tell me how to do what I want

For example, if I have the file "a.htm":

The content of "a.htm":

<iframe id="frame1" src="f.htm" />

and the content of "f.htm" is:

<input type="textbox" id="tbx1" />
<input type="textbox" id="tbx2" />
<input type="submit" id="okbtn" value="ok"/>

For example, when the page "a.htm" loads, I want the textbox ("tbx1" for example) in the page "f.htm", (contained in the frame "frame1") to be filled by some text.

thanks in advance

Jim O'Brien
  • 2,512
  • 18
  • 29
Amer Sawan
  • 2,126
  • 1
  • 22
  • 40

1 Answers1

3

Before I start on this answer, it's worth explaining that you cannot use JavaScript alone to manipulate a frame which serves content from a separate domain*. In other words, if your page, a.htm, is on your domain (e.g. http://yourdomain.com/a.htm), and f.htm is on another domain (e.g. http://someothersite.com/f.htm), then JS will not be able to access anything inside the iframe.

*There are techniques available to do this, but as a rule of thumb they cause more trouble than good.

However, if it's all on the same domain you should have no trouble. In a.htm, to access the iframe, you could use the following jQuery snippet:

$(function() {
  var iframe = $('#frame1').load(function() {
    $(this).contents().find('#tbx1').val('Text in a box!');
  });
});

So, your a.htm could look like:

<html>
  <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
    <script type="text/javascript">
      $(function() {
        var iframe = $('#frame1').load(function() {
          $(this).contents().find('#tbx1').val('Text in a box!');
        });
      });
    </script>
  </head>
  <body>
    <iframe id="frame1" src="f.htm" />
  </body>
</html>
Jim O'Brien
  • 2,512
  • 18
  • 29
  • I don't quite understand what you're asking. What do you mean by 'a frame contained in the page'? – Jim O'Brien Sep 22 '11 at 16:33
  • thank you very much it was useful .... but are there any way to control frame from a separate domain !!???? – Amer Sawan Sep 24 '11 at 10:33
  • Check out [this answer](http://stackoverflow.com/questions/2067029/getting-around-same-origin-policy-in-javascript-without-server-side-scripts/2067240#2067240) - it's pretty much perfect – Jim O'Brien Sep 26 '11 at 12:00