On the topic of window
Standard browsers implement a Window
interface, from which a global window
property is exposed to javascript in the documents. Subsequent navigation will load different documents in the same Window
even if new tabs are opened. So the properties you use, like window.location
and window.history
inside your document, would be present in the Window
before a user navigates to your page (from Facebook) and therefore be available to your document.
This also applies to when you directly load your page in a new browser window - the document will have access to the window
property. More on Window
and window
here: https://developer.mozilla.org/en-US/docs/Web/API/Window
If you are worried about your page getting loaded by a non-standard browser, or for some reason, the window property's history
and location
properties are overridden, you could just do a check to see if they are available, before calling them with:
if (window && window.location && window.location.hash) {
// safely use window.location.hash here
}
But even then, the error would be suppressed by the browser on the client-side.
On the topic of using document.title
with replaceState()
The specification specifies it as a string, so by design, it will return an empty string if it is not set. There are no warnings from Mozilla for using it before a document is fully loaded. More here https://developer.mozilla.org/en-US/docs/Web/API/Document/title
Here are some quick tests I did to see if it is in fact the case using an HTML page with no <title>
tag.
<html>
<head>
<script>
console.log("title", document.title)
window.history.replaceState({}, document.title, "newHMTL.page");
</script>
</head>
<body>
Testing
</body>
</html>
There are no errors or warnings as expected.
On the topic of replaceState
The specification points out that most browsers ignore the title
/ document.title
parameter that you pass to replaceState
:
Most browsers currently ignore this parameter, although they may use
it in the future. Passing the empty string here should be safe against
future changes to the method. Alternatively, you could pass a short
title for the state.
So while I had a page ready, some more quick tests. Setting the title to null; undefined; and a function;
and then passing it to replaceState
did not change the title in the history nor throw errors in Chrome when there was a <title>
tag or not. So 6 tests.
<html>
<!-- <title>title</title> -->
<head>
<script>
let title = () => alert("huh?") //null; //undefined;
console.log("Title", title);
window.history.replaceState({}, title, "NewHTML.page");
//works as expected
</script>
</head>
<body>
Testing
</body>
</html>