2017-10-05 64 views
0

当我尝试检测URL包括hashtag的变化,例如:asp.net的MVC +检测包括hashtag变化

http://localhost:54345/Shop/Catalog?cg=9#3221

http://localhost:54345/Shop/Product#1241

jQuery的hashchange方法不火。

$(window).on('hashchange', function (e) { 
    //.... 
}) 

如果目录行动之间的井号标签的更改,然后检测。例如:

http://localhost:54345/Shop/Catalog?cg=9#3221

http://localhost:54345/Shop/Catalog?cg=9#2453

所以我的问题是,包括hashtag检测是不可能的不同请求之间?或不同的ASP.NET MVC操作?

回答

1

hashchange()仅适用于包括hashtag的变化相同页。

取决于当你想将事件触发,你可以针对第一页的unload()

$(window).bind('beforeunload', function() { 
    // do something before the page unloads 
}); 

或新的页面,这将简单地用ready()做的load()

$(window).ready(function() { 
    // do something after the new page loads 
}); 

如果后者去,你可以添加条件逻辑,检查对主题标记思慕后期效果相同:

if(window.location.hash) { 
    // Fragment exists 
} 

希望这有助于! :)