/** * jQuery hashchange 1.0.0 * * (based on jquery.history) * * modified by net.itcast.cn,make it easy to use * * Copyright (c) 2008 Chris Leishman (chrisleishman.com) * Dual licensed under the MIT (MIT-LICENSE.txt) * and GPL (GPL-LICENSE.txt) licenses. */ (function($) { $.fn.extend({ hashchange: function(callback) { this.bind('hashchange', callback); if (location.hash)//if location.hash is not empty,fire the event when load,make ajax easy { callback(); } }, openOnClick: function(href) { if (href === undefined || href.length == 0) href = '#'; return this.click(function(ev) { if (href && href.charAt(0) == '#') { // execute load in separate call stack window.setTimeout(function() { $.locationHash(href) }, 0); } else { window.location(href); } ev.stopPropagation(); return false; }); } }); // IE 8 introduces the hashchange event natively - so nothing more to do /* if ($.browser.msie && document.documentMode && document.documentMode >= 8) { $.extend({ locationHash: function(hash) { if (!hash)//get hash value { if (location.hash.charAt(0) == '#') { return location.hash.substr(1, location.hash.length-1); } return location.hash; } if (!hash) hash = '#'; else if (hash.charAt(0) != '#') hash = '#' + hash; location.hash = hash; } }); return; }*/ var curHash; // hidden iframe for IE (earlier than 8) var iframe; $.extend({ locationHash: function(hash) { if (!hash)//get hash value { if (location.hash.charAt(0) == '#') { return location.hash.substr(1, location.hash.length - 1); } return location.hash; } if (curHash === undefined) return; if (!hash) hash = '#'; else if (hash.charAt(0) != '#') hash = '#' + hash; location.hash = hash; if (curHash == hash) return; curHash = hash; if ($.browser.msie) updateIEFrame(hash); $.event.trigger('hashchange'); } }); $(document).ready(function() { curHash = location.hash; /* if ($.browser.msie) { // stop the callback firing twice during init if no hash present if (curHash == '') curHash = '#'; // add hidden iframe for IE iframe = $('<iframe />').hide().get(0); $('body').prepend(iframe); updateIEFrame(location.hash); setInterval(checkHashIE, 100); } else { setInterval(checkHash, 100); }*/ }); $(window).unload(function() { iframe = null }); function checkHash() { var hash = location.hash; if (hash != curHash) { curHash = hash; $.event.trigger('hashchange'); } } /* if ($.browser.msie) { // Attach a live handler for any anchor links $('a[href^=#]').live('click', function() { var hash = $(this).attr('href'); // Don't intercept the click if there is an existing anchor on the page // that matches this hash if ($(hash).length == 0 && $('a[name=' + hash.slice(1) + ']').length == 0) { $.locationHash(hash); return false; } }); }*/ function checkHashIE() { // On IE, check for location.hash of iframe var idoc = iframe.contentDocument || iframe.contentWindow.document; var hash = idoc.location.hash; if (hash == '') hash = '#'; if (hash != curHash) { if (location.hash != hash) location.hash = hash; curHash = hash; $.event.trigger('hashchange'); } } function updateIEFrame(hash) { if (hash == '#') hash = ''; var idoc = iframe.contentWindow.document; idoc.open(); idoc.close(); if (idoc.location.hash != hash) idoc.location.hash = hash; } })(jQuery);
JQuery HashChange插件修改
在做AJAX的时候前进、后退按钮的处理是比较重要的,可以使用location.hash来解决这个问题。原理这里不再重复,为了方便,我使用了JQuery的HashChange插件,不过这个插件用起来不好:$.locationHash()方法只能设置hash,不能读取hash,不符合JQuery的风格;hashchange事件在用户通过地址栏直接敲“a.htm?#a”这种方式的时候不会触发,必须在ready的时候做处理。
我对这个插件做了修改代码如下:
(function($) {
$.fn.extend({
hashchange: function(callback) {
this.bind('hashchange', callback);
if (location.hash)//if location.hash is not empty,fire the event when load,make ajax easy
{
callback();
}
},
openOnClick: function(href) {
if (href === undefined || href.length == 0)
href = '#';
return this.click(function(ev) {
if (href && href.charAt(0) == '#') {
// execute load in separate call stack
window.setTimeout(function() { $.locationHash(href) }, 0);
} else {
window.location(href);
}
ev.stopPropagation();
return false;
});
}
});
// IE 8 introduces the hashchange event natively - so nothing more to do
if ($.browser.msie && document.documentMode && document.documentMode >= 8) {
$.extend({
locationHash: function(hash) {
if (!hash)//get hash value
{
if (location.hash.charAt(0) == '#') {
return location.hash.substr(1, location.hash.length-1);
}
return location.hash;
}
if (!hash) hash = '#';
else if (hash.charAt(0) != '#') hash = '#' + hash;
location.hash = hash;
}
});
return;
}
var curHash;
// hidden iframe for IE (earlier than 8)
var iframe;
$.extend({
locationHash: function(hash) {
if (!hash)//get hash value
{
if (location.hash.charAt(0) == '#') {
return location.hash.substr(1, location.hash.length - 1);
}
return location.hash;
}
if (curHash === undefined) return;
if (!hash) hash = '#';
else if (hash.charAt(0) != '#') hash = '#' + hash;
location.hash = hash;
if (curHash == hash) return;
curHash = hash;
if ($.browser.msie) updateIEFrame(hash);
$.event.trigger('hashchange');
}
});
$(document).ready(function() {
curHash = location.hash;
if ($.browser.msie) {
// stop the callback firing twice during init if no hash present
if (curHash == '') curHash = '#';
// add hidden iframe for IE
iframe = $('<iframe />').hide().get(0);
$('body').prepend(iframe);
updateIEFrame(location.hash);
setInterval(checkHashIE, 100);
} else {
setInterval(checkHash, 100);
}
});
$(window).unload(function() { iframe = null });
function checkHash() {
var hash = location.hash;
if (hash != curHash) {
curHash = hash;
$.event.trigger('hashchange');
}
}
if ($.browser.msie) {
// Attach a live handler for any anchor links
$('a[href^=#]').live('click', function() {
var hash = $(this).attr('href');
// Don't intercept the click if there is an existing anchor on the page
// that matches this hash
if ($(hash).length == 0 && $('a[name=' + hash.slice(1) + ']').length == 0) {
$.locationHash(hash);
return false;
}
});
}
function checkHashIE() {
// On IE, check for location.hash of iframe
var idoc = iframe.contentDocument || iframe.contentWindow.document;
var hash = idoc.location.hash;
if (hash == '') hash = '#';
if (hash != curHash) {
if (location.hash != hash) location.hash = hash;
curHash = hash;
$.event.trigger('hashchange');
}
}
function updateIEFrame(hash) {
if (hash == '#') hash = '';
var idoc = iframe.contentWindow.document;
idoc.open();
idoc.close();
if (idoc.location.hash != hash) idoc.location.hash = hash;
}
})(jQuery);
$.locationHash()是读取当前页面的hash,$.locationHash("a")是设置当前页面的hash,$(window).hashchange()监听hash改变事件(在第一次绑定事件的时候如果页面有hash值,则立即触发一次事件)。
例子:
$(window).hashchange(function() {
alert($.locationHash());
});
$("input").click(function() {
$.locationHash($(this).val());
});
完整的例子,一个在服务器端进行数字加倍运算:
<script type="text/javascript">
var calcFinish = function(data) {
$("#msg").text(data);
}
$(function() {
$("#btn1").click(function() {
//改变hash
$.locationHash($("#txt1").val());
});
$(window).hashchange(function() {
var i = $.locationHash();//获得hash
if (i) {//发出计算请求
$.post("CalcService.ashx", {"i":i},calcFinish);
}
});
});
</script>
<input type="text" id="txt1" /><span id="msg"></span>
<input type="button" id="btn1" value="calc" />
使用的时候注意前进、后退按钮和地址栏的变化。
相关推荐
jQuery DOM元素哈希插件 支持的哈希方法 base64 md5 sha1 自己编译 如果要编译自己的版本或jquery.hash的修改 npm install node compile 抓取out/jquery.hash.min.js文件并将其插入! 例子 DOM元素 $elem = $ ...
jQuery 哈希标签输入 用于编写哈希标签的 jquery 插件 ##Demo
亲测好用,加密结果与php的md5函数结果一直,使用时只需引入文件,然后用$.md5调用
本文实例讲述了jQuery md5加密插件jQuery.md5.js用法。分享给大家供大家参考,具体如下: 使用方法: $.(md5("你想要加密的字符串")); jquery.md5.js插件代码: /** * jQuery MD5 hash algorithm function * *...
门户最热门搜索词、论坛热门搜索这个都是后台控制添加的,后台->全局->搜索设置->热门关键词,添加即可 这里需要注意一个特殊情况,如果开启了纵横搜索,可能无法添加关键词,那么只能在模板里面直接添加 打开...
#jQuery序列化哈希插件 在麻省理工学院(MIT)许可下由托管。 jQuery插件,该插件从表单或任何DOM元素的序列化返回哈希值。 它在嵌套哈希的输入名称上支持方括号。 如果要从表单获取值并将其与另一个哈希合并,...
jquery-pjaxr 是一个插件,它使用 ajax 和 pushState 来提供快速浏览体验。 它能够用完整的后退功能替换多个容器和不同的头部标签。 对于 pjaxr 的完全降级。 终于发布了 1.2 版,但还有很多工作要做,目前我没有...
Chroma-Hash是一个能够为用户提供更好密码输入体验的jQuery插件。它能够将输入的值转换成某种颜色组合。用户只要记住正确密码的颜色,就能够判断自己输入的密码是否正确,而不需要等到提交服务器才知道是否正确。 ...
一个让你很简单处理url的jquery插件. A simple, lightweight url parser for JavaScript (~1.6 Kb minified, ~0.6Kb gzipped). url(); // ...
前端项目-jquery.hashcash.io,jquery hashcash.io插件
jQuery.scrollToHash 工作正在进行中 !!!!! 可定制的滚动到哈希 jQuery 插件#Todos 抵消管理 滚动持续时间 宽松 打回来 目标检查 url重写真/假 速度管理 URL ... 调用插件###HTML选项 1:触发元素是一个链接<a>Go
Feedify 是个 jQuery 插件,用来把文本转换成 HTML feed ,类似 Twitter 和 Facebook。它能转换 URL 到链接,添加用户链接和 Hash 搜索链接。在线演示 标签:Feedify
历史的jQuery插件可以帮助您回到您的JavaScript支持应用程序/前进按钮和书签。 You can store the application state into URL hash and restore the state from it.你可以存储到应用程序状态的网址散列和恢复它的...
Chroma-Hash是一个能够为用户提供更好密码输入体验的jQuery插件。它能够将输入的值转换成某种颜色组合。用户只要记住正确密码的颜色,就能够判断自己输入的密码是否正确,而不需要等到提交服务器才知道是否正确。
Redis hash操作 Redis 集合set 和有序集合操作 Redis 集合操作补充 Redis 发布订阅及本节作业 第12周 上节回顾 数据库介绍 mysql基本使用 mysql数据类型与创建表 mysql 增删改查 mysql 外键关联 mysql 连接查询 ...
滚动到JQuery 插件用于在不同事件期间(垂直)滚动到特定元素。 属性[data-scroll-to-this] 将此属性添加到将滚动到的元素[data-scroll-to-this="onload"] 页面加载时滚动到该元素[data-scroll-to-this="onclick"] ...
1、jQuery实现的内链接平滑滚动 不需要使用太复杂的插件,只要使用下载这段代码即可实现基于内部链接的平滑滚动 $('a[href^="#"]').bind('click.smoothscroll',function (e) { e.preventDefault(); var anchor = ...
INTRO 简介这是一个实现满屏滚动的插件,可以配合jquery和zepto使用页面结构三层结构,参考demoOPTIONS 配置 var list = $('.fullswitch-container .fullswitch-pagelist'); //使用插件时结构层次须与本例一致,但不...
jquery hash change event plugin: Internet Explorer 8, Firefox 3.6+, 和Chrome 5+里,已经提供了[removed]事件,但是在老版本的浏览器中并没有这个事件,这个插件通过定时器判断hash是否产生了变化,以便在老...
JCaptcha4Struts2 是一个 Struts2的插件,用来增加验证码的支持,使用时只需要用一个 JSP 标签 (<jcaptcha:image label="Type the text "/> ) 即可,直接在 struts.xml 中进行配置,使用强大的 JCaptcha来生成验证码...