如何使用JavaScript检查当前URL是否以:#!/about
或#!/ask
结束?
window.location.hash
包含当前哈希码,所以基本上:
if(window.location.hash === '#!/about') {
// Do something
} else if(window.location.hash === '#!/ask') {
// Do something else
}
你可以随意使用。
编辑:正如zzzzBov指出的,如果你需要一些jQuery,把它放在一个$(document).ready
处理程序。div;)
使用jQuery获取当前url
$(location).attr('href');
下面是代码片段
jQuery(document).ready(function() {
var url=$(location).attr('href');
var match1 = arr.match('#!/about');
var match2 = arr.match('#!/ask');
if(match1){
alert("match1 found");
} else if{
alert("match2 found");
}
});