views:

1137

answers:

5

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.

A: 

try

$(window.parent.document).dolink($(this).attr('href'));

Kind Regards --Andy

jAndy
doesn't work on any browser
Pier Luigi
A: 

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>
Alsciende
doesn't work on any browser
Pier Luigi
right, forgot something
Alsciende
+1  A: 

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.

Pier Luigi
A: 

you should call code like that

if(function != undefined) {

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

}

azeem
A: 

Just to share that Pier's observation is right it doesn't work on a local setting and has to be on a web server.

I found another irritating issue, it does not work across different domains. E.g. Parent file on server A and iframe file on server B. The parent javascript object returned in the iframe file is most likely referenced back to itself.

If anyone has a solution to my issue do let me know, thanks!

Nathaniel