1

I am developing a Silverlight application which is going to be hosted inside another's application IFrame:

<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<script type="text/javascript">
    function testMethod() {
        alert(1);
    }       
</script>
<iframe src="URL of the silverlight application" width="750" height="500"/>

This is just a dummy app for testing purposes (I am using the default MVC 4 template - this is actuly the Contacts.cshtml file)

An this is the part where I call the JavaScript function.

HtmlPage.Window.Invoke("testMethod");

Any suggestions what to do/what I am doing wrong?

Ivan Crojach Karačić
  • 1,911
  • 2
  • 24
  • 44
  • Are you getting any errors in the javascript console? Did you enable the HTMLBridge: http://msdn.microsoft.com/en-us/library/cc645023(v=vs.95).aspx – Emond Dec 19 '11 at 13:13
  • Yes I did (now) and still nothing... I am pretty new to web developement so can't be that precise :/ Both are hosted on localhost atm (localhost/MvcApplication1 and localhost/SilvelrightApp) – Ivan Crojach Karačić Dec 19 '11 at 13:27
  • So you did set the `enableHtmlAccess` parameter? – Emond Dec 19 '11 at 13:30
  • Just saw that the original application had also a `testMethod` which was empty so that's why it wasn't called in the first place... Now I am getting an exception that the testMethod is not defined... – Ivan Crojach Karačić Dec 19 '11 at 13:59
  • Have a look at this question: http://stackoverflow.com/questions/251420/invoking-javascript-in-iframe-from-parent-page – Emond Dec 19 '11 at 14:06

2 Answers2

1

Try:-

ScriptObject parent = HtmlPage.Window.GetProperty("parent") as ScriptObject;
if (parent != null)
{
     parent.Invoke("testMethod");
}
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
0

This is what has worked for me in this case

HtmlPage.Window.Eval("if(typeof window.parent.testMethod== 'function'){window.parent.testMethod()};");

I know that .Eval should be avoided but it looks like it's the only way for me :/

Ivan Crojach Karačić
  • 1,911
  • 2
  • 24
  • 44
  • It would be appropriate to resolve why you need to resort to .Eval? On the basis of the above the my answer ought to work too. Hence something is still to be understood. – AnthonyWJones Dec 20 '11 at 12:19