Skip to content

Instantly share code, notes, and snippets.

@xiadd
Created April 25, 2016 07:41
Show Gist options
  • Save xiadd/ec7ed287f2cf94605e72b051c7051d03 to your computer and use it in GitHub Desktop.
Save xiadd/ec7ed287f2cf94605e72b051c7051d03 to your computer and use it in GitHub Desktop.
html history api

pushState里的title并没有什么卵用

(function (doc, win) {

  function Router () {
    this.state = {};
    this.events = [];
  }
  //添加popstate事件
  win.addEventListener('popstate', function (e) {

  })

  Router.prototype = {
    on: function (ev, callback){
      this.events.push(ev)
    },
    set: function (url, title, callback){
      history.pushState({url: url}, title, url);
      this.state[url]=1;
    },
    check: function (url){
      return this.state.url?true:false;
    },
    delete: function (url){
      if(this.check(url)){
        delete this.state.url
      }else {
        return ;
      }
    },
    setTitle: function (title) {
      title === 'string' ? title = title : title = title.toString();
      document.title = title
    },
    refresh: function () {

    }
  }

  win.Router?win.Router:win.Router=Router;
})(document, window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment