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. Chromium on Android: Android在系统Chromium为了实现主消息循环分析

    总结:刚开始接触一个Chromium on Android时间.很好奇Chromium主消息循环是如何整合Android应用. 为Android计划,一旦启动,主线程将具有Java消息层循环处理系统事 ...

  2. JavaEE(13) - JPA属性映射

    1. 映射实体的属性 #1. 使用@Transient修饰不想持久保存的Field #2. 使用@Enumerated修饰枚举类型的Field #3. 使用@Lob, @Basic修饰枚举类型的Fie ...

  3. android在单身的对象和一些数据的问题被释放

    正式接触android我们一直在开发了一段时间,该项目的第一个版本最终会很快结束. 当有它自己的测试.拥有android后台.同一时候打开了几个应用之后又一次切回到自己的app.发现报错了.经过排查, ...

  4. CSS+DIV+JQuery实际的视频汇总

    在牛腩中学习过CSS和DIV.在JS中学习过CSS+JS 封装,在这里又回想了一遍,也作为查漏补缺,也算比較系统的复习了一遍吧. css核心内容主要包含四个方面:标准流.盒子模型,浮动和定位,而在B/ ...

  5. spring4.2完整web项目(使用html视图解析器)

    完整配置springmvc4,最终视图选择的是html,非静态文件. 最近自己配置spring的时候,遇到很多问题,由于开发环境和版本的变化导致网友们给出的建议很多还是不能用的,可能还会有很多人会遇到 ...

  6. linux_安装_安装编译phantomjs 2.0的方法_转

    项目中要对数据公式webkit渲染,phantmjs 2.0的效果好比1.9好不少. 安装过程中 坑比较多. 转载文章: phantomjs 2.0最新版的官方不提供编译好的文件下载,只能自己编译,有 ...

  7. oracle_常用命令(表空间查询)

    ---查询表空间 对应的物理文件,名称 ,大小 ,已用的,利用率 SELECT B.FILE_NAME "FILE_NAME", B.TABLESPACE_NAME "T ...

  8. monkey命令详解

    标准的monkey 命令 adb shell monkey [options] <eventcount> 例如: adb shell monkey -v    产生500次随机事件,作用在 ...

  9. Postman 是一个非常棒的Chrome扩展,提供功能强大的API & HTTP 请求调试

    Postman 是一个非常棒的Chrome扩展,提供功能强大的API & HTTP 请求调试   需要FQ才能安装,使用时应该不用FQ了,除非使用postman的历史记录功能:   非常棒的C ...

  10. 【转】Android 常用 adb 命令总结

    原文地址:http://testerhome.com/topics/2565 针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Andr ...