Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
React: window.close() working in Chrome but not Firefox

I have a single-page react app hosted on github pages, which comes with many limitations. In order to get my "Login with Twitter" flow working, I found it was necessary to open the dynamic twitter login url in a new tab, give the Twitter API an endpoint on my backend as the callback URL, and then have my backend redirect the user to the root url of my frontend client with the generated JWT token in the URL as a query (/?token=abcd1234). I then have some code on the landing page of my client that looks like this:

useEffect(() => {

        // at /?close, just close the window
        if (props.location.search === '?close') {
            window.close()
            closeWindow()
            setShowCloseMessage(true)

        // at /?token, save the token to localstorage, then close the window
        } else if (tokenUrlRE.test(props.location.search)) {
            const token = props.location.search.match(tokenRE)[0]
            localStorage.setItem('token', token)
            window.close()
            closeWindow()
            setShowCloseMessage(true)
        }

    }, [])

This works fine in Chrome, but not Firefox - perhaps Firefox is more stringent about requiring that Windows only be closed by the script that opened them, and Chrome allows any script on the same domain that opened a window to close it?

Anyway, several other answers here on Stackexchange recommended the following code, which I added inside the closeWindow() function appearing in the above code:

function closeWindow() {
            console.log('clicked close')
            window.open('', '_parent', '')
            window.close()
        }

But this also did not work in Firefox. Furthermore, this additional solution I found caused React to crash:

open(location, '_self').close()

Is there any way to accomplish what I want with Firefox? For the time being I have simply used that showClose state variable seen in the first code block to display a "Sign-in successful, please return to the previous tab" message, but this is less than ideal.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!