Is it feasible to use JavaScript to modify the title of a page in the browser history even after the page has finished loading? This functionality should work consistently across different browsers, including those that do not support the HTML5 History API. My main focus is on Chrome.
I have experimented with various methods, but I haven't found a reliable solution yet. The last attempt involved using history.js:
<html>
<head>
<title>Standard title</title>
<script src="https://rawgit.com/browserstate/history.js/master/scripts/bundled-uncompressed/html4%2Bhtml5/native.history.js"></script>
</head>
<body>
<script>
window.setTimeout(function(){
document.title = "My custom title";
History.replaceState({}, "My custom title", window.location.href);
}, 3000);
</script>
</body>
</html>
If I load this page in Chrome within 3 seconds of its initial load, I see the title as Standard title
, but after 3 seconds, it changes to My custom title
.
The reason behind this requirement is that I am working on a JavaScript-only application (Angular 1) that operates in multiple environments (dev, test, production). I want to display a title like MyApp (<environmentName>)
, without building separate app versions for each environment. Instead, the app retrieves environment information from the backend through AJAX and updates the page title accordingly. Although the title updates successfully, the browser history still reflects the original "standard" title.
Is there a solution to update both the displayed title and the title in the browser history simultaneously?