Giter VIP home page Giter VIP logo

url-change-event's Introduction

url-change-event

a wrapper event that listen & control URL changes δΈ­ζ–‡

Installation

you can install with npm install url-change-event

    /* in ES 5 */
    require('url-change-event')
    /* in ES 6 */
    import 'url-change-event'

or

    <script src="url-change-event.js"></script>

Due to override some history method, you should import this lib before your code.

Usage

window.addEventListener('urlchangeevent', function(e) {
    // your code here
})

UrlChangeEvent instance

Properties

  • oldURL {URL} - the url before change.
  • newURL {URL | null} - the url after change. WARNING: when event.action is beforeunload, this value is null.
  • action {[pushState|replaceState|popstate|beforeunload]} - the action that causes the url to change.

Method

  • preventDefault - prevent url change

License

MIT licensed

url-change-event's People

Contributors

camsloanftc avatar dependabot[bot] avatar jerrykingxyz avatar kazzarin avatar nelson-liu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

url-change-event's Issues

popstate event listener

I have two questions about popstate.

  1. Why is stopImmediatePropagation called here? The popstate event is fired after the pushstate event is fired, so the stopImmediatePropagation is called, will cause the routing switch to fail.

https://github.com/jerrykingxyz/url-change-event/blob/master/src/index.js#L10

20221209-152154

  1. Why do you need to call history.go when you cancel here?

https://github.com/jerrykingxyz/url-change-event/blob/master/src/index.js#L24

In response to the above two questions, I think the popstate event handling implementation may look like this

window.addEventListener('popstate', function (e) {
    initState();
    const nowIndex = globalWindow.history.state._index;
    const nowURL = new URL(globalWindow.location.href);
    if (nowIndex !== cacheIndex) {
      const notCanceled = globalWindow.dispatchEvent(
        new UrlChangeEvent({
          oldURL: cacheURL,
          newURL: nowURL,
          action: 'popstate',
        }),
      );
      if (!notCanceled) {
        e.stopImmediatePropagation();
      }
      updateCacheState();
    }
  });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.