JSFiddle - React, Tailwind, and code Playground

by Jagi

HTML

<input type="text" id="input">

JavaScript

const input = document.getElementById("input");

history.pushState(null, null, document.URL);

const preventBack = function () {
  const answer = confirm('Do you want to leave?');
	if (!answer) {
  	history.pushState(null, null, document.URL);
  }
};

input.addEventListener('focus', function() {
	window.addEventListener('popstate', preventBack);
});

input.addEventListener('blur', function() {
	window.removeEventListener('popstate', preventBack);
});