xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

前端监控: 用户页面停留时间计算

hashchange

// event handler
function locationHashChanged() {
  if (location.hash === '#cool-feature') {
    console.log("You're visiting a cool feature!");
  }
}

window.onhashchange = locationHashChanged;
// event listener
function hashListener() {
  console.log('The hash has changed!');
}

window.addEventListener('hashchange', hashListener, false);

```js
// test, 覆盖 hash
function changeHash() {
  location.hash = (Math.random() > 0.5) ? 'location1' : 'location2';
}

// Let this snippet run before your hashchange event binding code
if (!window.HashChangeEvent)(function(){
  var lastURL = document.URL;
  window.addEventListener("hashchange", function(event){
    Object.defineProperty(event, "oldURL", {enumerable:true,configurable:true,value:lastURL});
    Object.defineProperty(event, "newURL", {enumerable:true,configurable:true,value:document.URL});
    lastURL = document.URL;
  });
}());

test


hashchange = HashChangeEvent {isTrusted: true, oldURL: "http://localhost:8080/about?hash=%2Fabout%23abc#123", newURL: "http://localhost:8080/about?hash=%2Fabout%23abc#666", type: "hashchange", target: Window, …}bubbles: falsecancelBubble: falsecancelable: falsecomposed: falsecurrentTarget: Window {window: Window, self: Window, document: document, name: "", location: Location, …}defaultPrevented: falseeventPhase: 0isTrusted: truenewURL: "http://localhost:8080/about?hash=%2Fabout%23abc#666"oldURL: "http://localhost:8080/about?hash=%2Fabout%23abc#123"path: [Window]returnValue: truesrcElement: Window {window: Window, self: Window, document: document, name: "", location: Location, …}target: Window {window: Window, self: Window, document: document, name: "", location: Location, …}timeStamp: 310272.22499999334type: "hashchange"__proto__: HashChangeEvent

refs

https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onhashchange



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/14166716.html