Is there any way to use javascript to monitor changes in the browser's URL?
The functions I want to implement are
1. Prevent certain URLs from jumping, which are on the blacklist.
2 Cannot jump in certain states. For example, jumping is not allowed when loading certain ajax resources.
But it must be global. The project has been formed, and there are various jump methods.
There are a tags, window.location.href, backend...
So we can only use the common ones. The action of changing the url is a rollback. Can JavaScript implement this?
Or is there any other way to achieve it
There is only this event onbeforeunload, but it cannot be controlled at will
Attempting to use js to hijack the user's browser will eventually fail. Access control on the server side is the right way
If it is your own project, you can make a unified jump method (fn)....
StackOverflow Answers
Additional to what was said on the first floor,
onbeforeunload
虽好,但只有PC端支持,不适用于移动端。单一维度来看,至多只能监听『页面是否注销』,无法监听『页面是否后退』;
即使你使用了
history.length
属性,由于history.length
Only records the characteristics of the total length, but it is still impossible to know whether the page moves forward or backward.As for forward and backward on the mobile side, pure js cannot achieve it, but it can be done with the help of native. Please refer to my analysis of why the return logic of Web App is so complicated | louis blog