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

163
Views
popstate event listener in Tampermonkey script only works after I click within the page

I have a Tampermonkey script, it should prevent the back action if the user clicked the back button of the browser. I'm using code from the answers to this question.

// ==UserScript==
// @name         Prevent touchpad gestures/button of the page back action
// @namespace    https://github.com/remi-crystal
// @version      0.1
// @description  Use google.com for testing propose
// @author       Remi Crystal
// @match        https://*.google.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    // always works
    console.log(document.title, location.href);

    // only works after any interaction
    window.addEventListener('popstate', function (event) {
      history.pushState(null, document.title, location.href);
    });
    history.pushState(null, document.title, location.href);
})();

This event-listener works fine in general. But if I didn't interact like clicking or pressing a key within the page (moving my cursor doesn't count), it won't work (the page just goes back if I click on the back button or use the touchpad gesture). Other code doesn't matter.

Why could this happen? How do I solve this problem?

My browser version: Chrome 96.0.4664.110 (Official Build) (arm64)

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!