Phi*_*sen 43 html javascript url fragment-identifier
背景:我有一个HTML页面,可以让您扩展某些内容.由于只需要加载页面的一小部分进行此类扩展,因此通过JavaScript完成,而不是通过指向新的URL/HTML页面.然而,作为奖励,用户能够永久链接到这样的扩展部分,即向其他人发送诸如的URL
并为其他用户立即打开"foobar"类别.这使用parent.location.hash ='foobar',因此该部分很好.
现在的问题是:当用户在页面上关闭此类别时,我想再次清空URL片段,即将http://example.com/#foobar转到http://example.com/以更新永久链接显示.但是,这样做parent.location.hash = ''会导致重新加载整个页面(例如在Firefox 3中),我想避免这种情况.使用window.location.href = '/#'不会触发页面重新加载,但会在URL中留下一些看起来不太好看的"#"符号.那么在流行的浏览器中有没有一种方法可以删除包含"#"符号的URL锚点,而不会触发页面刷新?
Mat*_*mas 49
正如其他人所提到的,HTML5 中的replaceState可用于删除URL片段.
这是一个例子:
// remove fragment as much as it can go without adding an entry in browser history:
window.location.replace("#");
// slice off the remaining '#' in HTML5:
if (typeof window.history.replaceState == 'function') {
history.replaceState({}, '', window.location.href.slice(0, -1));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47246 次 |
| 最近记录: |