1. Try onHashChange to listen only for forward and backward events and hash changes
2. Try onbeforeUnload events that fire just before leaving the current page (refresh or close). 1) Close the browser window 2) go to another page through the address bar or favorites 3) click back, forward, refresh, home page one of the time 4) click a url to connect to another page 5) click the event triggered jump but can not get the URL change, and only support PC side.
3. Add a custom event
history.pushState = ( f => function pushState(){
var ret = f.apply(this, arguments);
window.dispatchEvent(new Event('pushstate'));
window.dispatchEvent(new Event('locationchange'));
return ret;
})(history.pushState);
history.replaceState = ( f => function replaceState(){
var ret = f.apply(this, arguments);
window.dispatchEvent(new Event('replacestate'));
window.dispatchEvent(new Event('locationchange'));
return ret;
})(history.replaceState);
window.addEventListener('popstate',()=>{
window.dispatchEvent(new Event('locationchange'))
});
Copy the code
Added listening for historical changes to hash changes.
4. Try nginx IP interception record, but the external chain does not access our server at all, so this scheme also does not work.
5. The following functions are encapsulated according to CSDN
let middleJump = function (el = 'a') { $(el).on('click', function (e) { let href = $(this).attr('href') || ''; url = ''; HrefRegExp (href)? (url = href) : (url = url + '? target=' + encodeURIComponent(href)); e.preventDefault(); href && window.open(url, '_blank'); }); }; Let hrefRegExp = function (href) {let reg = "" // return reg.test(href); };Copy the code
Note: if this method is used globally, it will listen to all A tags, which may cause performance problems, but it can be used on pages where users use custom A tags
6. Use jquery.ajaxstart
This approach is similar to the interception of a request VUE and can be used in advance of all Ajax requests, but cannot be used because the URL changes cannot be retrieved.