I am trying to push a state to the browser history, when clicking the text in the following div element:
<!DOCTYPE html>
<head>
</head>
<body>
<div onclick="onNavigate('/tester')">link</div>
<script>const onNavigate = path => history.pushState("","", window.location.pathname + path)</script>
</body>
</html>
I get an
Uncaught DOMException error: Failed to execute 'pushState' on 'History': A history state object with URL 'Failed to execute 'pushState' on 'History': A history state object with URL 'file://[path-to-file]/index.html/tester' cannot be created in a document with origin 'null' and URL 'file://[path-to-file]/index.html'.' cannot be created in a document with origin 'null' and URL 'file://[path-to-file]/index.html'.
There are plenty similar posts related to pushState()
and replaceState()
- like this one History pushState throws Uncaught DOMException but none of them provide a working solution.
I have also tried with location.href
instead of location.pathname
without success.
What's wrong?