【问题标题】:Close popup window if user log out in angular如果用户以角度注销,则关闭弹出窗口
【发布时间】:2019-02-20 02:44:17
【问题描述】:

在我的应用程序中,有一个页面用户可以打开一个弹出窗口。当用户单击注销时,它必须关闭弹出窗口。 我在Global.ts 类中使用静态变量来存储弹出窗口变量

public static QUICKTREND : any;

在打开弹出窗口的函数中,我存储了它

this.quickWin = window.open(URL, 'QuickTrend', 'width=' + w + ', 
height=' + h + ', left=' + x + ', top=' + y + ', location=no'+ ', status=no'); 
 Constants.QUICKTREND = this.quickWin;

在 logout() 函数中,我弹出窗口并关闭

if(!isNullOrUndefined(Constants.QUICKTREND)){
       let currentIframe = Constants.QUICKTREND;
       currentIframe.close();
 }

如果我不刷新页面,它就可以正常工作。

但是当我刷新页面时,变量 QUICKTREND 重置为未定义。

如果页面刷新,我搜索了保持变量的解决方案,只有保存到 localStorage 或 sessionStorage 的解决方案。但是这样一来,我无法保存弹出窗口对象,因为它是 DOM 对象,Converting circular structure to JSON 错误显示。

localStorage.setItem("currentIframe", this.quickWin);

是否可以将弹出窗口保存到 localStorage?

如果页面刷新,我如何在注销时关闭弹出窗口?

【问题讨论】:

  • (您的代码似乎与 iframe 没有任何关系;它们与弹出窗口不同。)
  • @Daniel Back 我的问题非常不同,角度与 javascript 和刷新页面
  • 将内容从 iframe 更新到弹出窗口
  • 您的问题与该问题相同;角度与答案无关——无论有没有框架,技术都是一样的。

标签: javascript angular typescript iframe


【解决方案1】:

你可以试试下面的代码:

app.component.ts

import { Component, HostListener } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Angular-Experiment';

  public myWindow;
  private URL = 'https://www.google.com/';

  @HostListener('window:beforeunload', ['$event'])
  closeOnrefresh(event) {
    console.log('closeOnrefresh', event);
    if (this.myWindow && !this.myWindow.closed) {
      this.myWindow.close();
    }
  }

  openWindow() {
    console.log("open window");
    if (!this.myWindow || (this.myWindow && this.myWindow.closed)) {
      this.myWindow = window.open(this.URL, 'self', 'width=1000,height=1000');
    } else {
      this.myWindow.focus();
    }
  }

  Winfocus() {
    console.log("Winfocus");
    if (this.myWindow && !this.myWindow.closed) {
      // this.myWindow.close();// to close
      this.myWindow.focus();// to focus
    }
  }

  Logout() {
    console.log("WinLogout");
    if (this.myWindow && !this.myWindow.closed) {
      this.myWindow.close();// to close
    }
  }
}

app.component.html

<input type="button" value="Open" (click)="openWindow()">
<input type="button" value="focus" (click)="Winfocus()">
<input type="button" value="Logout" (click)="Logout()">

【讨论】:

  • 我也希望仅在用户单击注销时关闭弹出窗口
  • 我使用 Angular 而不是 javascript
  • 这里使用的函数/元素并非特定于 Javascript,它们也可以在 Angular 中使用。最后,角度代码被转换为 javascript。所以,我认为这不会有太大的不同。
  • 他们非常不同
  • 感谢您提供的 Eashwar 示例,但这不是我的要求。我希望它在刷新页面后保持弹出窗口,仅在用户单击注销时关闭。
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多