search

Home  >  Q&A  >  body text

Front-end - javascript global monitoring address url changes

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

漂亮男人漂亮男人2774 days ago674

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-05-19 10:40:49

    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

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-19 10:40:49

    If it is your own project, you can make a unified jump method (fn)....


    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:40:49

    StackOverflow Answers

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-19 10:40:49

    Additional to what was said on the first floor, onbeforeunload虽好,但只有PC端支持,不适用于移动端。
    单一维度来看,至多只能监听『页面是否注销』,无法监听『页面是否后退』;
    即使你使用了history.length属性,由于history.lengthOnly 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

    reply
    0
  • Cancelreply