(function () { const baseLink = 'backredirect/index.html'; function buildTargetUrl(base) { const trimmed = base.trim(); const query = document.location.search.replace('?', '').toString(); if (!query) return trimmed; const sep = trimmed.indexOf('?') > 0 ? '&' : '?'; return trimmed + sep + query; } function setBackRedirect(url) { const target = buildTargetUrl(url); history.pushState({}, '', location.href); history.pushState({}, '', location.href); history.pushState({}, '', location.href); window.addEventListener('popstate', () => { console.log('onpopstate', target); setTimeout(() => { location.href = target; }, 1); }); } setBackRedirect(baseLink); })();