0

I am creating a form using Jquery-ui. Buttons are in parent page and codes are in another page, which is embeded into parent page through iframe. can anyone tell me how can I invoke dialog function of Jquery-ui from parent page's button.

$( "#comment" ) 
.button()
 .click(function() 
{ parent.I1.f1(); });

//I1 is the frame id here is a button

<button id="comment" name="comment">Comment</button>

This is on the parent page.

In another page(Iframe page)

function f1(){
 $("#comment" ).dialog("open"); 
} 

is defined details and code of dialog box is here

jqueryui.com/demos/dialog/modal-form.html

  • 1
    possible duplicate of [Invoking javascript in iframe from parent page](http://stackoverflow.com/questions/251420/invoking-javascript-in-iframe-from-parent-page) – balexandre Sep 19 '11 at 07:22
  • I am able to reach iframe function. but from the iframe page i need to invoke this function $("#comment" ).dialog("open"); but it's not invoking.this function is in the iframe itself. – manish987654321 Sep 19 '11 at 07:25
  • does it throw an error? just wrap your call with a `try/catch` and `alert` the error – balexandre Sep 19 '11 at 07:32
  • without a proper test environment we can't help you further. – balexandre Sep 19 '11 at 07:39
  • you can edit your own post and add this info right? If it's part of the question, should always be there, not as a comment. – balexandre Sep 19 '11 at 09:38

2 Answers2

1

Give your iframe an ID (eg myiframe), and then do something like this:

document.getElementById('myiframe').contentWindow.function();

Where function() is the function you want to execute.

Remember that the page in your iframe needs to be on the same domain as the parent page for this to work.

Samich
  • 29,157
  • 6
  • 68
  • 77
gregdev
  • 1,863
  • 3
  • 23
  • 28
  • I am able to reach iframe function. but from the iframe page i need to invoke this function $("#comment" ).dialog("open"); but it's not invoking.this function is in the iframe itself. – manish987654321 Sep 19 '11 at 07:27
  • `document.getElementById('myiframe').contentWindow.$("#comment" ).dialog("open");` – Michael Mior Sep 19 '11 at 11:14
0

First, it will only work if the same domains are exactly the same, in other words, the iframe needs to belong to the same url, if not, a cross-browser exception will be thrown.

To call javascript methods on other frames, you can find thousands of articles out there for this, you simply read an existing answer

Invoking JavaScript code in an iframe from the parent page

Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342