【问题标题】:How to show opened tab window using javascript如何使用javascript显示打开的标签窗口
【发布时间】:2018-02-16 10:41:33
【问题描述】:

如何使用 javascript 显示上一个窗口。我有两个 html 页面,例如 test1.html 和 test2.html

test1.html

 <button id="newWindow">New Window</button>

test2.html

 <button id="prevPage">ok</button>

如果我从 test1.html 中单击 newWindow 按钮,它将转到新选项卡窗口中的 test2.html。

如果我从 test2.html 页面单击 prevPage 按钮,它将关闭当前窗口选项卡并应该转到 test1.html 选项卡窗口。

     $(document).on('click', '#newWindow', function () { 
     window.open("test2.html"); 
     });

     $(document).on('click', '#prevPage', function () {

     /*close pressent window(test2.html)*/
     window.close();

     /*open the test1.html tab window*/

     window.opener('test1.html','parent').focus();

     });

【问题讨论】:

  • 您可以尝试将上一页保存在本地存储中并访问它,据我所知历史 API 不提供以前状态的信息。
  • 嗯,如果你使用普通的超链接而不是window.open(),prev url 不会显示在历史 API 中吗?
  • 过去有一个功能可以将当前和以前的字符串显示为只读字符串,developer.mozilla.org/en-US/docs/Web/API/History 但在某些情况下已经过时,我在 api 中没有看到任何标准解决方案案例...
  • 您可以使用 window.location.href 看到当前,但您看到我无法知道过去的字符串...按照 OP 的要求

标签: javascript jquery


【解决方案1】:

我很确定您需要保持第一个窗口仍然打开才能打开一个新窗口,否则在此之前 js 函数进程会丢失。所以你应该先打开你的新窗口,然后关闭原来的窗口。像这样:

$(document).on('click', '#newWindow', function () { 
    /*open test2.html*/
    window.open('test2.html').focus();

    /*close test1.html*/
    window.close();
});

$(document).on('click', '#prevPage', function () {
    /*open test1.html*/
    window.open('test1.html').focus();

    /*close test2.html*/
    window.close();
});

【讨论】:

  • 这在 chrome 中不起作用:self.close(); var mypreWindow=window.open('test1.html','起始页'); mypreWindow.focus(); $("#transfer_funds").dialog("open");
  • window.open函数中不需要第二个参数。
【解决方案2】:

为什么不依赖历史对象和简单的写入

window.history.back();

【讨论】:

  • 你必须先打开前一个窗口然后关闭当前窗口。但是有一个问题......您是否完全有必要在单独的窗口中打开这个新页面。您可以在同一个窗口中打开,然后后退按钮将与历史记录一起使用。
猜你喜欢
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2014-12-26
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多