最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call parent window function in chrome browser? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Mar 31, 2010 at 6:14 Pier LuigiPier Luigi 7,8719 gold badges39 silver badges46 bronze badges 1
  • Is the iframe on the same domain as the parent document? – Max Shawabkeh Commented Mar 31, 2010 at 6:18
Add a comment  | 

4 Answers 4

Reset to default 9

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!')
}
发布评论

评论列表(0)

  1. 暂无评论