获得页面加载ajax的哈希网址 [英] get ajax hash url on page load
问题描述
这么说我设置散列当我做一个Ajax调用:
so say I set a hash when i do an ajax call:
例如: http://example.com/#hash.html
如果我加载其他页面,点击后退按钮,我如何检测哈希并提取负载的网址是什么? 其余的我得到了已经涵盖:)
if I load another page and click on the backbutton, how would i detect the hash and extract the url on load? The rest I got already covered :).
如果你需要更多的code,请告诉我。顺便说一句,我使用jQuery。
If you need more code, please tell me. btw, i'm using jquery.
推荐答案
有一个事件,的 hashchange
,但它仅支持在Firefox 3.6,IE8和我承担了最新的铬合金和狩猎。
There is an event, hashchange
, but it's only supported in Firefox 3.6, IE8 and I assume the latest Chromes and Safaris.
为了获得最佳的支持,检测 hashchange
事件,如果没有present,使用轮询功能与的setInterval()
。
For best support, detect the hashchange
event, and if not present, use a polling function with setInterval()
.
所以,你会做这样的事情......
So you would do something like...
(function() {
var hash = window.location.hash;
if ('onhashchange' in window) {
window.onhashchange = hashChanged;
} else {
setInterval(function() {
if (window.location.hash != hash) {
hash = window.location.hash;
hashChanged();
}
}, 500);
}
var hashChanged = function() {
alert('Hash has changed!');
};
})();
这篇关于获得页面加载ajax的哈希网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!