jquery javascript:使用主题标签添加浏览器历史记录?
我的网站上有一个链接,上面写着“全屏谷歌地图”。单击它后,我会将 google 地图加载到 100% 宽和 100% 高位置固定 div
容器中。单击链接时,我还会添加 #map 作为哈希。
是否可以使浏览器后退按钮与此一起使用?也就是说,如果我单击此链接,我会将 #map 添加到我当前的地址。单击后退按钮后,#map 哈希将被删除,并且带有 google 地图的 div
容器将被删除或隐藏。
这有可能吗?
编辑:
$('.showMapLink').live('click', function() {
$('#mapContainer').fadeIn('fast', function () {
loadMap("mapContainer");
top.location.hash = "map";
$(window).bind( 'hashchange', function( event ) {
$('#mapContainer').fadeOut('fast', function () {
$(this).children().remove();
})
});
});
});
I have a link on my website that says "Fullscreen Google Map". Once I click it, I load a google map into a 100% wide and 100% high position fixed div
container. When the link is clicked, I also add a #map as hash.
Is it possible to make the browser back button work with that? That is, if I click this link, I add #map to my current address. Once I click the back button, the #map hash is removed and the div
container with the google map is removed or hidden.
Is that somehow possible?
edit:
$('.showMapLink').live('click', function() {
$('#mapContainer').fadeIn('fast', function () {
loadMap("mapContainer");
top.location.hash = "map";
$(window).bind( 'hashchange', function( event ) {
$('#mapContainer').fadeOut('fast', function () {
$(this).children().remove();
})
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个很好的资源和插件可以帮助解决这个问题,那就是 Ben Almans bbq 插件,它将帮助您设置和读取 url 的哈希部分(例如参见
pushState
和getState
),它提供了一个跨浏览器工作的hashchange
事件。处理
hashchange
事件并在其中进行处理。您需要在页面第一次加载时手动触发该事件。A great resource and plugin to help with this is Ben Almans bbq plugin, It will help you set and read the hash part of the url (eg see
pushState
andgetState
) and it provides ahashchange
event that works across browsers.Handle the
hashchange
event and do your processing in there. You need to manually trigger the event the first time the page loads.也许,你的问题一半在这里得到了解答: jQuery moving hash value from URL
Probably, half of your question is answered here: jQuery removing hash value from URL
是的,(使用最新的)浏览器是可能的。
请参阅
document.location.hash
将#map
添加到当前 URL。注册一个
onhashchange
事件侦听器来查找更改,但请注意,当您设置标记时,它也会引发此类事件。因此,您需要丢弃包含与您刚刚设置的哈希值相同的哈希值的任何事件。或者,查看 jQuery 历史记录插件,它具有针对旧版浏览器的解决方法。
Yes, it's possible (with recentish) browsers.
See
document.location.hash
to add#map
to the current URL.Register an
onhashchange
event listener to look for changes, but note that when you set the tag, it also raises such an event. Hence you need to discard any event which contains the same hash as the one you just set.Alternatively, look at the jQuery history plugin, which has workarounds for older browsers.