location.hash和back history [英] location.hash and back history

查看:190
本文介绍了location.hash和back history的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个函数可以调用以防止浏览器在更改哈希值时记录后历史记录条目?

I我正在写一个简单的javascript库,当用户浏览每个图像时,它会更改浏览器URL而无需重新加载页面。

I am writing a simple javascript gallery that changes the browser url without reloading the page as the user moves through each image.

这是通过将location.hash设置为唯一来完成的。图片的ID。

This is done by setting the location.hash to the unique ID of the image.

window.location.hash = imageID;

问题是,当用户点击浏览器后退按钮时,他们必须向后移动每个图像,如它是页面加载。

The problem is when the user hits the browser back button, they must move backwards through every image like it was a page load.

如果他们使用图库旋转了20张图片,他们必须再次点击21次才能返回上一页。

If they rotated through 20 images using the gallery, they must click back 21 times to get back to the previous page.

如何防止使用javascript记录回溯历史记录?

推荐答案

window.location.replace 将允许您设置网址而不将其添加到浏览器历史记录中。

window.location.replace will let you set the url without adding it to the browser history.

var baseUrl = window.location.href.split('#')[0];
window.location.replace( baseUrl + '#' + imageID );

文档

这篇关于location.hash和back history的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆