簡體   English   中英

如何關閉由window.open打開的窗口?

[英]How to close window opened by window.open?

如何關閉由window.open打開的窗口? 目前,我有一個帶有處理OAuth的服務器端的reactjs應用。

流程如下:1)用戶單擊按鈕並打開一個新窗口,該窗口重定向到服務器端(服務器端重定向到身份驗證服務)。 2)服務器端在身份驗證成功后向客戶端發出套接字io事件,以及收到的用戶信息。3)通過window.open()打開的窗口最終將被關閉。

相關代碼粘貼在下面

componentDidMount() {
  const {
    socket,
    provider
  } = this.props
  console.log('component did mount')

  socket.on(provider, user => { // Event listener
    // Window should be closed at this stage after listening to event.
    console.log(user)
    this.setState({
      user
    })
    this.props.addUser(user.name, 10)
  })
}

startAuth() {
  const {
    provider
  } = this.props

  const width = 600,
    height = 600
  const left = (window.innerWidth / 2) - (width / 2)
  const top = (window.innerHeight / 2) - (height / 2)
  const url = `https://localhost:5000/${provider}`

  return window.open(url, '', // Open window to server side
    `toolbar=no, location=no, directories=no, status=no, menubar=no, 
          scrollbars=no, resizable=no, copyhistory=no, width=${width}, 
          height=${height}, top=${top}, left=${left}`
  )
}

我試圖在socket.on()中執行window.close(),但是它關閉了錯誤的窗口,而不是window.open()打開的窗口。 我還嘗試在發出socket io事件后在服務器端添加window.close(),但它也沒有做任何事情。 任何幫助深表感謝。

您需要保留對打開的窗口的引用,並在其上調用close。

const opened = startAuth();

opened.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM