Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在为window.location附加更改通知程序_Javascript_Reflection - Fatal编程技术网

Javascript 正在为window.location附加更改通知程序

Javascript 正在为window.location附加更改通知程序,javascript,reflection,Javascript,Reflection,每当window.location.href发生更改时,我都需要触发一个函数,但我遇到了问题。我查看了各种手表polyfills的源代码,但我不能完全了解代码的情况 if (!Object.prototype.watch) { Object.defineProperty(Object.prototype, "watch", { enumerable: false , configurable: true , writable: false , value: f

每当window.location.href发生更改时,我都需要触发一个函数,但我遇到了问题。我查看了各种
手表
polyfills的源代码,但我不能完全了解代码的情况

if (!Object.prototype.watch) {
  Object.defineProperty(Object.prototype, "watch", {
    enumerable: false
    , configurable: true
    , writable: false
    , value: function (prop, handler) {
      var
        oldval = this[prop]
        , newval = oldval
        , getter = function () {
          return newval;
        }
        , setter = function (val) {
          oldval = newval;
          return newval = handler.call(this, prop, oldval, val);
        }
        ;
      if (delete this[prop]) { // can't watch constants
        Object.defineProperty(this, prop, {
          get: getter
          , set: setter
          , enumerable: true
          , configurable: true
        });
      }
    }
  });
}
显然,我对JS或反射的内部结构了解不多。以下是一种适用于标题的方法:

var observer = new window.MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    change({title: mutation.target.textContent});
  });
});

observer.observe(document.querySelector('head > title'),
  { subtree: true, characterData: true, childList: true });

但是我不能通过查询选择器指定位置,我非常确定它需要实现节点类,以便observer工作。

这是一个小技巧,但并不比我见过的任何其他东西都多,而且它在所有现代浏览器中的工作方式几乎相同

var myTitle = document.title;
var myLocation = window.location.href;

setInterval(function() {
    if (myTitle != document.title || myLocation != window.location.href)
    {
        // Do something here;

        myTitle = document.title;
        myLocation = window.location.href;         
    }
}, 100);

它只是在短时间内轮询属性。简写为用户可见,但也不会有太多的CPU开销。

在非Firefox浏览器中,您不能在
location.href
上设置观察者。我需要将观察者设置为
location.pathway
location.hash
、和
location.search

最好是重用一个函数,而不是将同一个函数写两次。我可以把它简化成这样:

if (!Object.prototype.watch) { //don't overwrite gecko watch function
  Object.prototype.watch = function(prop, handler) {
    var oldval = this[prop], newval = oldval,
      getter = function() {
        return newval;
      },
      setter = function(val) {
        oldval = newval;
        return newval = handler.call(this, prop, oldval, val);
      };
    if (delete this[prop]) {
      Object.defineProperty(this, prop, {
        get: getter,
        set: setter
      });
    }
  };
}

这对你有用吗?我无法在location.pathname上定义setter我不记得了,例如。。。完全没有P