Merennulli
Merennulli

Reputation: 35

Invoking function in iframe on same-domain, supporting Internet Explorer

I have an event on the parent page that triggers a javascript function, which in turn calls another function on the child iframe. Both pages are in the same domain and I have verified this by having javascript display the document.domain in both pages simultaneously.

This is what has worked for years, and is suggested in similar topics: windows.frames["iframeID"].somefunction(); That ceased working with Internet Explorer version 8.0.7600.16385 It does work on Internet Explorer version 8.0.6001.18702

I've tried replacing it with each of these and the result was the same: iframeName.somefunction(); document.frames("iframeName").somefunction(); top.frames[0].somefunction();

The error given is "Access is denied" and references the first bit of code in somefunction(), however, somefunction() works fine if called within the iframe itself.

Is there a new "proper" way to call functions within an iframe from the parent? Is there a workaround on the code side (I can't control the user's browser)?

EDIT: Here is the internal function: function somefunction() { somebutton.click(); } If instead I use "alert('something');", it works without error.

EDIT2: Newer version, as well as IE9's Beta have extended this to also defeat the hacky workaround, so I'm back to dead in the water.

EDIT3: I had forgotten about this until someone posted nearly a year later. This is the workaround I ended up with:

Parent calls iframename.badscripthack(); Child in iframe has function: function badscripthack() { document.getElementById("OffscreenTextField").focus(); } function submitthis() { document.getElementById("SubmitButton").click(); } And the tag:

As the name implies, this is a bad hack, but it was necessary. Of course, it doesn't answer the "proper" way to handle it. It has worked since, and nobody "back-tabs" to trigger it accidentally (and if they did, the consequences are merely saving a form early).

Upvotes: 3

Views: 2571

Answers (1)

Amit Bagga
Amit Bagga

Reputation: 648

Parent Html

<html>
<head>
 <script type="text/javascript">
  function CallFrame()
  {
        var frame = document.getElementById("myframe");
    if(frame)
    {
           frame.contentWindow.Show();
        }
  } 
</script>
</head>
<body>
<h1> I am Parent </h1>
<input type="button" onclick="CallFrame()" value="Call Frame function" /><br/>
<iframe id="myframe" src="Frame.html" />
</body>
</html>

Frame.Html

<html>
<head>
<script type="text/javascript">
function Show()
{
alert("Called");
}
</script>
</head>

<h1> I am IFrame</h1>
<input type="button" onclick="Show();" value="Call Own function">
</body>
</html>

Tested in FireFox and IE8

Upvotes: 1

Related Questions