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. MTK6572横屏的调试过程

    电视剧集:系统MTK缺省的系统源代码,Phone模式.底部有三个虚拟按键.需求为,设置成默认横屏,设定一个合理的虚拟按键方案. ------------------------------------ ...

  2. HDU 4123 Bob’s Race 树的直径+单调队列

    题意: 给定n个点的带边权树Q个询问. 以下n-1行给出树 以下Q行每行一个数字表示询问. 首先求出dp[N] :dp[i]表示i点距离树上最远点的距离 询问u, 表示求出 dp 数组中最长的连续序列 ...

  3. SQL Server 备份和还原

    SQL Server 备份和还原   SQL Server 备份 恢复模式 SQL Server 数据恢复模式分为三种:完整恢复模式.大容量日志恢复模式.简单恢复模式. 完整恢复模式 默认的恢复模式, ...

  4. java自动转型

    /*2015-10-30*/ public class TypeAutoConvert { public static void main(String[] args) { int a = 5; Sy ...

  5. poj 2804 字典 (特里 要么 快排+二分法)

    2804:词典 总时间限制:  3000ms  内存限制:  65536kB 描写叙述 你旅游到了一个国外的城市.那里的人们说的外国语言你不能理解.只是幸运的是,你有一本词典能够帮助你. 输入 首先输 ...

  6. Android 使用DexClassLoader要执行其他apk方法

    Android在apk文件dex文件是java编译出来.class次打包,当然在打包之前会利用自己的协议做一些数据处理,比如优化函数表和变量表.在java程序中是使用classloader来载入这些编 ...

  7. rman(上)

    CHANGE命令更改备份和副本的状态  1.change backupset 100 unavailable    CATALOG命令是用来备份的碎片和复制信息到RMAN数据库  1.息增加到RMAN ...

  8. uva 10652 Board Wrapping (计算几何-凸包)

    Problem B Board Wrapping Input: standard input Output: standard output Time Limit: 2 seconds The sma ...

  9. POJ1743---Musical Theme(+后缀数组二分法)

    Description A musical melody is represented as a sequence of N (1<=N<=20000)notes that are int ...

  10. Android物业动画研究(Property Animation)彻底解决具体解释

     前p=1959">Android物业动画研究(Property Animation)全然解析具体解释上已经基本展示了属性动画的核心使用方法: ObjectAnimator实现动画 ...