I found the solution after some testing, so here giving back to the community.
The below row of code only works one time for a reason I don't understand.
Replacing that line of code with the line underneath solved the issue.
var container = document.getElementsByClassName("mx-name-container5");
var container = $(".mx-name-container5");
I found the jQuery selector method using this link:
A popstate event is dispatched to the window each time the active history entry changes between two history entries for the same document. If the activated history entry was created by a call to history.pushState(), or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.