javascript – 关闭弹出窗口后刷新父窗口

前端之家收集整理的这篇文章主要介绍了javascript – 关闭弹出窗口后刷新父窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在创建一个到hello.html的弹出窗口.当我关闭弹出窗口(hello.html)时,我想要我的原始(父页面)重新加载.我似乎无法让它上​​班,但我很接近.这是我到目前为止的主页和hello.html页面代码….
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript">
  5. function open_win()
  6. {
  7. window.open("hello.html","_blank","toolbar=yes,location=yes,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=no,copyhistory=yes,width=400,height=400");
  8. }
  9. </script>
  10.  
  11.  
  12. <script language="JavaScript">
  13.  
  14. function refreshParent() {
  15. window.opener.location.href = window.opener.location.href;
  16.  
  17. if (window.opener.hello.html)
  18.  
  19. {
  20. window.opener.hello.html.close()
  21. }
  22. window.close();
  23. }
  24. </script>
  25.  
  26.  
  27. </head>
  28.  
  29. <body>
  30.  
  31. <script type="text/javascript">
  32.  
  33. var d=new Date();
  34. document.write(d);
  35.  
  36. </script>
  37.  
  38. <form>
  39. <input type="button" value="Open Window" onclick="open_win()">
  40. </form>
  41. </body>
  42. </html>

这是hello.html …

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script type="text/javascript">
  5. </script>
  6. </head>
  7. <body>
  8.  
  9. Hello
  10.  
  11. </body>
  12. </html>

解决方法

在子窗口中订阅 unload event,从子窗口调用父窗口通知它正在关闭

编辑添加代码示例…

  1. function popupClosing() {
  2. alert('About to refresh');
  3. window.location.href = window.location.href;
  4. }
  5.  
  6. var w = window.open("hello.html",height=400");
  7. w.onunload = function () {
  8. window.parent.popupClosing()
  9. };

猜你在找的JavaScript相关文章