hash和history模式实现原理

4 篇文章 0 订阅
两种风格的实现原理
  1. hash 模式:通过改变location.hash( 注:只改变url的hash值而不是url的主体部分,顾不会刷新页面、不会发送http请求),然后由浏览器监听事件onhashchange事件来监听hash值的变化并触发绑定的回调函数,从而来展示不同的页面内容。
    以下是一个简单的hash路由的实列代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <metacharset="UTF-8">
    <title>router</title>
</head>
<body>
<ul>
    <li><ahref="#/">turn white</a></li>
    <li><ahref="#/blue">turn blue</a></li>
    <li><ahref="#/green">turn green</a></li>
</ul>
<script>
functionRouter() {
        this.routes = {};
        this.currentUrl = '';
    }
    Router.prototype.route = function(path, callback) {
        this.routes[path] = callback || function(){};
    };
    Router.prototype.refresh = function() {
        this.currentUrl = location.hash.slice(1) || '/';
        this.routes[this.currentUrl]();
    };
    Router.prototype.init = function() {
        window.addEventListener('load', this.refresh.bind(this), false);
        window.addEventListener('hashchange', this.refresh.bind(this), false);
    }
    window.Router = newRouter();
    window.Router.init();

    var content = document.querySelector('body');
    // change Page anythingfunctionchangeBgColor(color) {
        content.style.backgroundColor = color;
    }

    Router.route('/', function() {
        changeBgColor('white');
    });
    Router.route('/blue', function() {
        changeBgColor('blue');
    });
    Router.route('/green', function() {
        changeBgColor('green');
    });
</script></body></html>
  1. HTML5 history模式:通过history interface 新增的pushState、replaceState方法以及现有的go、back、forward方法来改变url(注:可以改变url的主体部分,顾在直接访问嵌套路由时,必须配有该路径所对应的资源否则会出现404的情况,但可以通过vue的redirect重定向到index页面或者404页面,来解决此问题),然后通过window.popState事件 来监听url变化并执行对应的回调函数,从而来展示不同的页面内容。
    以下是一个简单的history路由的实列代码:

class Router {
  constructor() {
    this.routes = new Map();
    this.init();
  }
  change(e) {
    // 防止为null
    const { path } = e.state || {};
    this.implement(path);
  }
  init() {
    window.addEventListener("popstate", this.change.bind(this));
    window.addEventListener("load", () => {
      const { pathname } = location;
      history.replaceState({ path: pathname }, "", pathname);
      this.implement(pathname);
    });
  }
  implement(path) {
    if (!this.routes.has(path)) {
      return;
    }
    const fn = this.routes.get(path);
    typeof fn == "function" && fn.call(this, path);
  }
  go(num) {
    history.go(num);
  }
  route(state, fn) {
    this.routes.set(state, fn);
  }
  push(state) {
    history.pushState({ path: state }, "", state);
    this.implement(state);
  }
  replace(state) {
    history.replaceState({ path: state }, "", state);
    this.implement(state);
  }
}

以下是使用方法:

<ul>
    <li><ahref="/">hash1</a></li>
    <li><ahref="/hash2">hash2</a></li>
    <li><ahref="/hash3">hash3</a></li>
</ul>
<div>
    <buttonclass="f">前进</button>
    <buttonclass="b">后退</button>
</div>
const color = {
  "/": "yellow",
  "/hash2": "#333",
  "/hash3": "#DDD"
};
const route = newRouter();
route.route("/", function(e) {
  document.body.style.background = color[e];
});
route.route("/hash2", function(e) {
  document.body.style.background = color[e];
});
route.route("/hash3", function(e) {
  document.body.style.background = color[e];
});
Array.from(document.links).forEach(fn => {
  fn.addEventListener("click", e => {
    e.preventDefault();
    const href = fn.href;
    const { pathname } = newURL(href);
    route.push(pathname);
  });
});
const backOff = document.querySelector(".b");
const forward = document.querySelector(".f");
backOff.addEventListener("click", () => route.go(-1));
forward.addEventListener("click", () => route.go(1));
hash、history两种风格的区别

hash

history

url带有#号

url中没有#号

兼容ie8

兼容ie10

只改变loaction.hash值,即#号后端的值 不改变url,不需要服务器做任何处理

改变url主体部分,适合单页面应用(vue,angular,react),否则每次修改url都需要服务器端根据配置返回对应的html到浏览器

刷新不会出现404

非单页面应用,直接访问嵌套路由会报404需要在服务器端做好对应路由的回调配置,而单页面应用如vue,如果访问的url,无法被本地路由配置所匹配则会报错,然后跳入本地配置的404页面

优缺点
  1. hash 模式:
    优点:兼容ie8,只改变hash值来触发hashchange,不需要服务端做任何处理;
    缺点:hash模式中的#号也称作锚点,这里的的 # 和 css 中的 # 是一个意思,所以在 hash 模式内,页面定位会失效;hash 不利于 SEO(搜索引擎优化);白屏时间问题。浏览器需要等待 JavaScript 文件加载完成之后渲染 HTML 文档内容,用户等待时间稍长。

  1. history模式:
    优点:更适合单页面应用,增加了pushstate,replacestate方法,操作扩展性更强大;
    缺点:无法兼容ie8 ,且vue框架本身就不支持ie8,因为ie8 浏览器无法模拟的 ECMAScript 5 特性;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值