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

253
Views
How To Use 'beforeunload' to Capture Iframe Click?

I am trying to build a code that does a request when a cross-origin iframe is clicked and it directs the main window to a new page.

Since it's impossible to directly tap into Iframe click events, I thought of the following conditions as necessary:

  • page unload event occurs
  • the window is out of focus

Page unload (As far as I know) happens only when the current url is directed to some other url.

Now, this unload could happen by clicking any link. To restrict it to Iframes, I added the condition of window being out of focus.


addEventListener('beforeunload',(event) =>{
    if(!(document.hasFocus())){
        // Do Something
    }
});

My question is, are their any limitations to this approach? If yes, then please suggest some more conditions to make it as close to reality.

For those of you, who are curious: I want to track clicks on Google AdSense Iframes on my website.

about 4 years ago · Santiago Trujillo
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!