Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 591
Pier Luigi Avatar asked Mar 31 '10 06:03

Pier Luigi


People also ask

How do I get a parent window?

To obtain a window's owner window, instead of using GetParent, use GetWindow with the GW_OWNER flag. To obtain the parent window and not the owner, instead of using GetParent, use GetAncestor with the GA_PARENT flag.

How do I view the parent window variable in a child window?

You just need to prefix “ window. opener. ” and write the same code that you will write in the parent window's HTML page to access its elements.

Can iframe access parent windows?

The window will get opened by using the iframe, and the communication could be done with the parent window from the child window. To call a parent window function, use the following syntax and also refer to the code given below.


2 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.

like image 113
Pier Luigi Avatar answered Oct 15 '22 17:10

Pier Luigi


you should call code like that

if(function != undefined)
{
 $(function() {
      $('a.arglink').click(function() {
         window.parent.dolink($(this).attr('href'));
         return false;
      });
 });
}
like image 4
azeem Avatar answered Oct 15 '22 18:10

azeem