如何使用jQuery检测URL更改 [英] How to detect URL changes with jQuery
本文介绍了如何使用jQuery检测URL更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
jQuery如何检测对网址的更改?
How can jQuery detect changes to a url?
例如:如果用户转到某个网页 site.com/faq/
没有显示,但是如果他去 site.com/faq/#open
jquery检测到它并做了一些事情。
For example: If a user goes to a page site.com/faq/
nothing shows, but if he goes to site.com/faq/#open
jquery detects it and does something.
推荐答案
您可以使用 hashchange 事件。
You can use the hashchange event.
function hashchanged(){
var hash = location.hash.replace( /^#/, '' );
//your code
}
window.addEventListener("hashchange", hashchanged, false);
$(function(){
// Bind the event.
$(window).hashchange(hashchanged);
// Trigger the event (useful on page load).
hashchanged();
});
function hashchanged(){
var hash = location.hash.replace( /^#/, '' );
//your code
}
这篇关于如何使用jQuery检测URL更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文