Front-end - javascript global monitoring address url changes
漂亮男人
漂亮男人 2017-05-19 10:38:49
0
4
587

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

漂亮男人
漂亮男人

reply all (4)
Peter_Zhu

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

        Peter_Zhu

        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

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!