如何检查当前URL是否以特定模式结束



如何使用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");
          }
});

最新更新