hashchange事件

需要解决的问题:
1.IE6/7及兼容模式下的IE8不支持onhashchange事件,(而且hash改变不会产生history)
    解决办法:用定时器来检测hash的变化;用隐藏的iframe调用document.write方法来产生历史;
2.hash的提取有兼容性问题:
   * IE6会取少一总分hash,
     如http://www.cnblogs.com/rubylouvre#stream/xxxxx?lang=zh_c
     IE6 > location.hash = #stream/xxxxx
     其他浏览器 > location.hash = #stream/xxxxx?lang=zh_c
   * firefox会对hash进行decodeURIComponent
     比如 http://www.cnblogs.com/rubylouvre/#!/home/q={%22thedate%22:%2220121010~20121010%22}
     firefox 15 => #!/home/q={"thedate":"20121010~20121010"}
     其他浏览器 => #!/home/q={%22thedate%22:%2220121010~20121010%22}
 
3.html5 history api 兼容pushState与replaceState, 研究一下history.js
 
 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
</head>
<body>
<a href="#a">a</a>
<a href="#b">b</a>
<a href="#c">c</a>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script>
(function(global, $) {
var hashchange = 'hashchange', doc = document, documentMode = doc.documentMode,
supportHashChange = ('on' + hashchange in window) && (documentMode === void 0 || documentMode > 7); var hash = '#', last_hash = getHash(), history_hash, timeoutID, delay = 50, iframe; function getHash(url) {
url = url || doc.location.href;
return '#' + url.replace(/^[^#]*#?(.*)$/, '$1');
} //为了产生历史
function setHistory(curHash, history_hash) {
var d = iframe.document;
if (curHash != history_hash) {
d.open();
d.write('<DOCTYPE html><html><body>' + curHash + '</body></html>');
d.close();
}
} function setup() {
if (!supportHashChange) {//IE6.7及混杂模式下的IE8,不支持onhashchange事件,所以采用iframe+定时器模拟
if (!iframe) {
var $el = $('<iframe tabindex="-1" width="0" height="0" style="display:none" title="empty"></iframe>');
$el.appendTo(doc.body);
iframe = $el[0].contentWindow;
$el.on('load', function() {
$el.off('load');
var d = iframe.document;
d.open();
d.write('<DOCTYPE html><html><body>' + hash + '</body></html>');
d.close();
timeoutID = setInterval(poll, delay);
});
}
function poll() {
hash = getHash();
history_hash = iframe.document.body.innerText;
if (hash != last_hash) {//当前窗口的hash改变
//console.log('current hash:' + hash + ',last hash:' + last_hash);
setHistory(last_hash = hash, history_hash);
//todo 在这里fire hashchange事件
} else if (history_hash != hash) {//如果按下回退健或前进健
//console.log('history hash:' + history_hash + ',current hash:' + hash)
location.href = location.href.replace(/#.*/, '') + history_hash;
}
}
}
}
setup();
})(window, $);
</script>
</body>
</html>

路由模块router实现step1的更多相关文章

  1. nodejs -Router

    Node 用 request 事件来处理请求响应,事件内使用分支语句处理不同路径的请求,而 Express 封装了这些操作,使得代码简洁优雅 但如果请求路径变多,都写在 app.js 文件里的话,就会 ...

  2. NodeJs学习日报day6——路由模块

    const express = require('express') const app = express() app.get('/user', function(req, resp) { resp ...

  3. 从前端中的IOC理念理解koa中的app.use()

    忙里偷闲,打开平时关注的前端相关的网站,浏览最近最新的前端动态.佼佼者,平凡的我做不到,但还是要争取不做落后者. 前端中的IoC理念,看到这个标题就被吸引了.IoC 理念,不认识呢,点击去一看,果然没 ...

  4. 结合 webpack 使用 vue-router(七)

    结合 webpack 使用 vue-router: 首先安装路由包vue-router: cnpm install vue-router 使用模块化工具导入 vue-router 后,必须手动调用 V ...

  5. 带你学Node系列之express-CRUD

    前言 hello,小伙伴们,我是你们的pubdreamcc,本篇博文出至于我的GitHub仓库node学习教程资料,欢迎小伙伴们点赞和star,你们的点赞是我持续更新的动力. GitHub仓库地址:n ...

  6. Angular 路由⑦要素

    cnzt       http://www.cnblogs.com/zt-blog/p/7919185.html http://www.cnblogs.com/zt-blog/p/7919185.ht ...

  7. react 项目全家桶构件流程

    资源:create-react-app.react.react-dom.redux.react-redux.redux-thunk.react-router-dom.antd-mobile/antd. ...

  8. 后端小白的VUE入门笔记, 进阶篇

    使用 vue-cli( 脚手架) 搭建项目 基于vue-cli 创建一个模板项目 通过 npm root -g 可以查看vue全局安装目录,进而知道自己有没有安装vue-cli 如果没有安装的话,使用 ...

  9. node.js 路由详解

    路由的基本使用 第一步:获取url跟目录下的字符 var http = require('http'); var url = require('url') http.createServer(func ...

随机推荐

  1. CodeForces 28D Don&#39;t fear, DravDe is kind dp

    主题链接:点击打开链接 为了让球队后,删除是合法的.也就是说,对于每一个车辆, l+r+c 一样,按l+r+c分类. 然后dp一下. #include <cstdio> #include ...

  2. Tick and Tick------HDOJ杭州电(无法解释,直接看代码)

    Problem Description The three hands of the clock are rotating every second and meeting each other ma ...

  3. org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案

    1.错误信息 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist ...

  4. 一对TCP协议及OSI简介模式

    原文地址:  移步这里

  5. Kinect SDK C++ - 2. Kinect Depth Data

    Today we will learn how to get depth data from a kinect and what the format of the data is kinect co ...

  6. Java串口通信详细解释

    前言 说到开源.恐怕非常少有人不挑大指称赞. 学生通过开源码学到了知识,程序猿通过开源类库获得了别人的成功经验及可以按时完毕手头的project,商家通过开源软件赚到了钱……,总之是皆大欢喜. 然而开 ...

  7. NYoj 最舒适的路线

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=711 分析:枚举速度最大的边,找出能够从S到达T的最大速度,然后求出它们的比值,与已 ...

  8. HDU 2612 -Find a way (注重细节BFS)

    主题链接:Find a Way 题目不难,前几天做,当时准备写双向BFS的,后来处理细节上出了点问题,赶上点事搁置了.今天晚上重写的,没用双向,用了两次BFS搜索,和双向BFS 道理差点儿相同.仅仅是 ...

  9. SQLServer 使用 @@ERROR

    原文:SQLServer 使用 @@ERROR 使用 @@ERROR 如果最后的 Transact-SQL 语句执行成功,则 @@ERROR 系统函数返回 0:如果此语句产生错误,则 @@ERROR ...

  10. swift 注意事项 (十六) —— 可选链

    可选链(Optional Chaining) 我们都知道"可选型"是什么.那么可选链又是什么,举个样例解释一下: struct MyName{      var name } st ...