How to call parent window function in chrome browser?

HI, I've surprisingly found problems, in Chrome browser, in calling window parent javascript functions. If I have a window with a javascript function defined in it

<script type="text/javascript">
  function dolink() {
   . . .
  }
</script>

and I have an iframe inside that window that makes this call using jquery

<script type="text/javascript">
 $(function() {
      $('a.arglink').click(function() {
         window.parent.dolink($(this).attr('href'));
         return false;
      });
 });
</script>

the call to dolink function doesn't work. Stepping with chrome javascript debugger, it appears that window.parent.dolink is undefined. It's by design or a mistake that I made? In Firefox and IE it works fine.

Answers


Finally found it!

It seems that Chrome browser doesn't permit to reference a parent window accessing pages with the file: protocol. In fact I tested above code with files on my machine, so with a url like file:///C:/mytests/mypage.html . If I put that page in a Web Server, it all works as expected.


you should call code like that

if(function != undefined)
{
 $(function() {
      $('a.arglink').click(function() {
         window.parent.dolink($(this).attr('href'));
         return false;
      });
 });
}

What about using frameElement and ownerDocument

<script type="text/javascript">
 $(function() {
      $('a.arglink').click(function() {
         window.frameElement.ownerDocument.parentWindow.dolink($(this).attr('href'));
         return false;
      });
 });
</script>

I just simply did this and it works for me

in iframe

function sendToParent() {
    parent.doSomething(); 
}
sendToParent();

in parent

function doSomething() {
    alert('did it!')
}

Need Your Help

Objective-C: enable "Open in..." on special condition

ios objective-c uti

I have a doubt, is it possible to enable the "Open in" only for a particular condition?

Object reference not set to an instance of an object when Invoking generic reflective method

c# exception generics reflection azure-mobile-services

So this problem is quite confusing. And its fairly complicated and abstract so I'm going to try my best to explain it.