厦门外贸人's Archiver

Jansfer 发表于 2006-3-6 15:24

如何配合IE的history历史记录用户操作历史

引用:[url]http://www.blogjava.net/emu/archive/2006/03/03/33518.html[/url]

Ajax七宗罪第五宗里面,作者讽刺Ajax需要一个“没有back和history的浏览器”。很讽刺吗?只是作者这么想罢了。至少要让Ajax应用支持Forward和Back,并不需要什么了不起的技术,我们也不需要这样的浏览器来改变用户习惯,事实上我们完全能做到让Forward和Back更酷。

这是一个示范页面:
index.htm

[code]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> emu's ajax bac,/forward sample </TITLE>
<META NAME="Author" CONTENT="emu">
<META NAME="Keywords" CONTENT="ajax history">
<META NAME="Description" CONTENT="这个例子演示了如何不跳转页面而配合IE的前进、后退按钮记录用户交互历史">
</HEAD>
<BODY>

<textarea cols=80 rows=30 id=t onkeypress="checkSave()">请在此处输入</textarea>

<iframe name=historyFrame width=0 height=0 src="hiddenpage.htm?0#0"></iframe>
<SCRIPT LANGUAGE="JavaScript">
<!--
historyFrame.history.go(-999)
var maxHistory=100;
var historyList=[];
var historyOffset=0;
var timer;
function checkSave(){
    if(timer)clearTimeout(timer);
    timer=setTimeout(saveHistory,500);
}
function saveHistory(){
    if(t.value==t.oldValue) return;
    historyList[historyOffset]=t.oldValue;
    historyOffset++;
    historyList.length=historyOffset
    t.oldValue=t.value;
    var url = historyFrame.location.protocol+"//"+historyFrame.location.pathname+"?";
    historyFrame.location=url+historyOffset+"#"+historyOffset;
    t.focus();
}
function gotoHistory(n){
    if(n<historyOffset){
        historyList[historyOffset]=t.value;
    }else if(n>historyOffset){
    }else return;
    historyOffset=n;
    t.value=historyList[historyOffset];
    t.focus();
}

window.onload=function(){
    t.oldValue=t.value;
}

//-->
</SCRIPT>
</BODY>
</HTML>
[/code]

需要一个简单的页面放在同一目录下面:
hiddenpage.htm

[code]
<HTML>
<BODY onload="gotoHistory()">
<SCRIPT>
function gotoHistory(){
    parent.gotoHistory(location.search.substr(1))
}
</SCRIPT>
</BODY>
</HTML>
[/code]
好了,打开index页面,在文本区域里面做一些输入,然后试试IE的前进和后退按钮能提供多酷的功能。

这个方法由emu的好朋友twinson首先提出来,以后我们就叫它twinson方法吧。

页: [1]
Google

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.