如何在点击退出按钮时,将页面重定向回从 window.location.href 打开的新 URL 的前一个 URL?

3

我有一个用于HTML按钮的代码:

<button class="btn btn-default" (click)="openUrl()"> Open </button>

在 app.component.ts 文件中,第一个页面是 https://example.com,我有以下代码:

openUrl(){
window.location.href = 'http://example.com/url';
}

http://example.com/url页面中有一个退出按钮。当他们点击退出按钮时,URL会更改为http://example.com/url/exit。然后,在15秒后应将其重定向到原始页面https://example.com
如何监听退出按钮的URL并将页面重定向回原始页面。问题在于我已经退出了原始页面。
2个回答

1
你可以使用iFrame,可能会做类似于这样的事情。
function removeIFrame() {
  var frame = document.getElementById("iframe");
  frame.parentNode.removeChild(frame);
}

function updateIframe(loc) {
   var frame = document.getElementById("frame1");
   console.info(frame.contentWindow);
   if (frame.contentWindow.location == "example.com/exit") {
     removeIframe();
   }
}

 function showIframe() {
    var ele = document.getElementById("frameWrapper");
    ele.style.display = "block";
 }

<div style="display:none;" id="frameWrapper">
    <iframe id="frame1" src="example.com/url" onLoad="updateIframe();"></iframe>
</div>

但是我该如何监听退出URL以使用history.go(-1)@Brian? - CodeVenture
@CodeVenture,你能修改退出页面上的代码吗? - Brian Patterson
1
@CodeVenture 或许可以使用 iframe?我更新了我的回答。 - Brian Patterson
我也在考虑同样的问题。但是我应该在哪里使用iframe呢? - CodeVenture
在输入console.info(frame.contentWindow)后,contentWindow被红色下划线标记,并且在悬停时显示“属性contentWindow在类型HTMLElement上不存在”。@Brian 在构建之后,仍然出现相同的错误。当我只输入frame.时,它不在属性列表中。 - CodeVenture
显示剩余15条评论

0

如果可以在新标签页中打开第二个页面,那么可以使用 window.open:

let newTab = window.open("//www.google.com", "_blank"); setTimeout(() => { newTab.close()},15000);

不,@Adam。它应该与 iframe 或 _self 在同一页上,而不显示任何原始页面的内容。 - CodeVenture

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接