window.location.href上的onchange事件

时间:2014-11-06 21:05:32

标签: javascript jquery

我想在window.location.href链接更改时触发一些Javascript函数。有可能吗?

这样的事情:

$(window.location.href).on('change', function(){
   doSomething();
});

2 个答案:

答案 0 :(得分:39)

你说'类似',给出了你可能正在寻找onbeforeunload事件处理程序的示例代码。

来自Mozilla docs

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

答案 1 :(得分:10)

The hashchange event occurs when there has been changes to the URL anchor part i.e after the #

window.addEventListener('hashchange', function() {

    alert("Hash Changed");

});
相关问题