asp.net mvc +检测标签更改

时间:2017-10-05 23:54:26

标签: jquery url hashtag asp.net-core-1.1

当我尝试检测url中的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

所以我的问题是,不同请求之间不能进行#标签检测?或不同的ASP.NET MVC操作?

1 个答案:

答案 0 :(得分:1)

hashchange()仅适用于同一页面的主题标签更改。

根据您希望触发事件的时间,您可以定位第一页的unload()

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

或新页面的load(),只需 ready() 即可完成:

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

如果使用后者,您可以添加条件逻辑,检查主题标签以模拟相同的效果:

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

希望这有帮助! :)