Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
596 views
in Technique[技术] by (71.8m points)

html - replaceState(): a history state with url ... cannot be created in a document with origin

I have window.history.replaceState(null, null, 'about'); in main.js which are located in required/javascripts on my server.

Then on the about page (located in / (root) on my server), I have a link that uses window.history.replaceState(null, null, 'about:me'); on this page. Everything works fine, but when I click on another link with the same function but with about:girlfriend as URL, I'm getting this error message:

Uncaught SecurityError: Failed to execute 'pushState' on 'History': A history state object with URL 'about:girlfriend' cannot be created in a document with origin 'http://my.domain.com'.

I don't know why my browser (latest version of Chrome) think I'm trying to reach this page using pushState and I don't know why I'm getting this error message, no matter how many times I've read it. Can someone please explain this for me? I do not use History.js for this.

Worth mention, is that I'm not getting this error message if I change : to something else, like - or /. I want to use : because / is not working (404 Page Not Found) and - doesn't fit in - it's better with :.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you are trying to do this locally, the following works for both locally and remote loaded pages:

history.replaceState(null,null, window.location.pathname + "your thing here")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...