I’d like to remove the #_=_
artefact that Facebook adds to URLs when a user logs in on my website.
I’m using this script:
if (window.location.hash === '#_=_') {
const uri = window.location.toString();
const withNoHash = uri.substring(0, uri.indexOf('#'));
window.history.replaceState({}, document.title, withNoHash);
}
I’d like the script to fire as soon as possible, so I’ve put it in the <head>
and it seems to work fine on Chrome & Firefox.
Is it standardized that the window.history
API is ready when the script executes in <head>
?
(and document.title
, by the way).
4
Answers
Yes it’s safe as the
window
object will be ready when the browser start parsing the head section.The
head
section is part of the DOM api andDocument
object is a property of thewindow
object and hence thedocument
will be loaded once the window object is ready.As the
history.replaceState
is part ofwindow
object, it’s safe of do any script part in head section.On the topic of
window
Standard browsers implement a
Window
interface, from which a globalwindow
property is exposed to javascript in the documents. Subsequent navigation will load different documents in the sameWindow
even if new tabs are opened. So the properties you use, likewindow.location
andwindow.history
inside your document, would be present in theWindow
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 onWindow
andwindow
here: https://developer.mozilla.org/en-US/docs/Web/API/WindowIf you are worried about your page getting loaded by a non-standard browser, or for some reason, the window property’s
history
andlocation
properties are overridden, you could just do a check to see if they are available, before calling them with:But even then, the error would be suppressed by the browser on the client-side.
On the topic of using
document.title
withreplaceState()
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.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 toreplaceState
:So while I had a page ready, some more quick tests. Setting the title to
null; undefined; and a function;
and then passing it toreplaceState
did not change the title in the history nor throw errors in Chrome when there was a<title>
tag or not. So 6 tests.It is pretty safe, but I would not use it in development without a
DOMContentLoaded
listener. Just to be extra safe and account for those who use older browsers, I advise that you add that listener forDOMContentLoaded
orwindow.onload
that way you won’t encounter any issues on a slower loading page. Also with thewindow.history
API, as it is a native function that requires no separate functionality, you may use that safely and immediately.TL;DR
Add a
DOMContentLoaded
listener just in case.Aloha) Yes, it’s safe for you and your clients because window is a global object what is available in browser for everyone and he can be used by any human. and your function can be executed by anyone. Don’t worry about security in that case 😉