用于URL上的斜杠(/)的Web更改哈希(#)

时间:2021-08-06 10:45:19

I'm making an "One-page navigation web" and I want to know if there's possible to replace hash character from an url to slash character. For example, I have this: www.example.com/#aboutus and I want to change for this: www.example.com/aboutus

我正在制作一个“单页导航网页”,我想知道是否有可能将网址中的哈希字符替换为斜线字符。例如,我有这个:www.example.com/#aboutus,我想改变这个:www.example.com/aboutus

But staying in same place, just changing the position to anchor link.

但是呆在同一个地方,只是改变位置来锚定链接。

I need to change .htaccess, or wich javascript (and derived) function is recommended.

我需要更改.htaccess,或者推荐使用javascript(和派生)函数。

Thanks in advance and have a nice day.

在此先感谢您,祝您度过愉快的一天。

1 个解决方案

#1


Assuming you mean SPA (Single-Page Application), you can take advantage of the History API to change the URL without actually changing pages.

假设您的意思是SPA(单页应用程序),您可以利用History API更改URL而无需实际更改页面。

window.history.pushState(stateObj, "About Us", "aboutus")

And, although I think it's supported by all major browsers, you still may want to keep the hash approach in mind for fallback should the API be absent (yes, I'm looking at you IE).

而且,尽管我认为它得到了所有主流浏览器的支持,但如果API不存在,您仍然可能需要记住哈希方法以便回退(是的,我正在看你的IE)。

Additional reference:

#1


Assuming you mean SPA (Single-Page Application), you can take advantage of the History API to change the URL without actually changing pages.

假设您的意思是SPA(单页应用程序),您可以利用History API更改URL而无需实际更改页面。

window.history.pushState(stateObj, "About Us", "aboutus")

And, although I think it's supported by all major browsers, you still may want to keep the hash approach in mind for fallback should the API be absent (yes, I'm looking at you IE).

而且,尽管我认为它得到了所有主流浏览器的支持,但如果API不存在,您仍然可能需要记住哈希方法以便回退(是的,我正在看你的IE)。

Additional reference: