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

javascript - How can I close a child window? - Stack Overflow

programmeradmin0浏览0评论

I'd like to provide a button on my child page that would close the child page from the child page itself. To make matters worse, I didn't write the child page - it is written using frames. So far I've tried this code in the child page:

Window.opener.location= '/parent page.html';
Window.close();

And, in the body:

<a href="JavaScript:if(confirm('close window?')) window.close()">close</a>

HELP!

I'd like to provide a button on my child page that would close the child page from the child page itself. To make matters worse, I didn't write the child page - it is written using frames. So far I've tried this code in the child page:

Window.opener.location= '/parent page.html';
Window.close();

And, in the body:

<a href="JavaScript:if(confirm('close window?')) window.close()">close</a>

HELP!

Share Improve this question edited Aug 19, 2012 at 9:21 Lee Taylor 7,99416 gold badges37 silver badges53 bronze badges asked Aug 19, 2012 at 5:16 user1609616user1609616 231 silver badge3 bronze badges 2
  • If it helps, the child page was created by google picasa. I'm able to get the word "close" to appear on the page, and the pop up window appears to confirm if I want to close the window, but nothing happens when I click "yes" – user1609616 Commented Aug 19, 2012 at 5:58
  • 1 You can close only those pages that were opened via Javascript. – Nivas Commented Aug 19, 2012 at 6:41
Add a ment  | 

2 Answers 2

Reset to default 5

As the link you inserted is inside an iframe, use window.top.close() instead of window.close(). window.top will refer to child window you created.

HTML for your hyperlink will be

<a href="javascript: if(confirm('close window')) {window.top.opener.location ='/auctioneer notes 8_22_12.html';window.top.close();}">close window</a>

This is cheating, but by refreshing the parent window the child window gets closed. Not very elegant but it works.

<form METHOD=post><p align=right><input TYPE="button" VALUE="Close This Window"onClick="window.parent.location.reload();"></p></form>
发布评论

评论列表(0)

  1. 暂无评论