gpt4 book ai didi

javascript - HTML5 历史 API 演示

转载 作者:搜寻专家 更新时间:2023-10-31 23:10:10 25 4
gpt4 key购买 nike

我一直在阅读有关 HTML5 历史 API 的信息,但到目前为止,我还没有找到一个简单的工作演示来展示代码的机制。

这是一个有效的 jsfiddle : 4个按钮和4个div。当用户按下一个按钮时,它会显示相应的面板。

我想做的是:

1) rewrite the URL so that when the user is on panel 4 the url ends with /Panel4
2) make the back button and forward button work with the history API.

我知道有 history.js 插件,但我想了解 API 如何以最简单的形式工作。

希望 jsfiddle 能帮助到此页面寻找代码演示的其他人。

谢谢。

最佳答案

好的,我为你做了这个例子。从 HTML 代码 (index.html) 开始:

<!DOCTYPE html>
<html>
<head>
<title>Stackoverflow</title>
<script type="text/javascript" src="sof.js"> </script>
</head>
<body onLoad="load();">
<ul id="menu">
<li><a href="/home">home</a></li>
<li><a href="/about">about</a></li>
<li><a href="/blog">blog</a></li>
<li><a href="/photos">photos</a></li>
</ul>
<button onclick="back ();">Back</button>
<button onclick="ff ();">Forward</button>
<div>
Action: <span id="action"></span><br/>
Url: <span id="url"></span><br/>
Description: <span id="description"></span>
</div>
</body>
</html>

然后是javascript文件(sof.js):

var menu, url, description, action, data, historyState, act;

function $ (id) {return document.getElementById (id);}

// Updates infos
function update (state) {
action.innerHTML = act;
url.innerHTML = state.url;
description.innerHTML = state.description;
}

// Goes back
function back () {
act = 'Back';
history.back ();
}

// Goes forward
function ff () {
act = 'Forward';
history.forward ();
}

function load () {
menu = $ ('menu');
url = $ ('url');
description = $ ('description');
action = $ ('action');

// State to save
historyState = {
home: {
description: 'Homepage'
} ,
about: {
description: 'Infos about this website'
} ,
blog: {
description: 'My personal blog'
} ,
photos: {
description: 'View my photos'
}
};

// This is fired when history.back or history.forward is called
window.addEventListener ('popstate', function (event) {
var hs = history.state;

if ((hs === null) || (hs === undefined)) hs = event.state;
if ((hs === null) || (hs === undefined)) hs = window.event.state;

if (hs !== null) update (hs);
});

menu.addEventListener ('click', function (event) {
var el = event.target;
// Prevents url reload
event.preventDefault ();

// Handles anchors only
if (el.nodeName === 'A') {
// Gets url of the page
historyState[el.innerHTML].url = el.getAttribute ('href');
// Creates a new history instance and it saves state on it
history.pushState (historyState[el.innerHTML], null, el.href);
act = 'Normal navigation';
update (historyState[el.innerHTML]);
}
});

// Handles first visit navigation
var index = location.pathname.split ('/');
index = index[index.length-1];
if (index !== '') {
historyState[index].url = location.pathname;
history.pushState (historyState[index], null, location.pathname);
act = 'First visit';
update (historyState[index]);
}
}

还有一个用于直接请求的 .htaccess:

RewriteEngine On

RewriteRule ^home$ ./index.html
RewriteRule ^about$ ./index.html
RewriteRule ^blog$ ./index.html
RewriteRule ^photos$ ./index.htm

每次单击 anchor 时,都会将一个新的历史实例插入历史堆栈并保存一个对象(称为状态):本地 url 已更改但加载会被“event.preventDefault()”停止方法。此外,一些信息(如 URL、描述和操作)已更新。

然后,使用“后退”和“前进”按钮,您可以浏览历史并使用“history.state”(或 event.state 或 window.event.state,取决于浏览器)检索当前状态.

最后,如果你直接在地址栏中输入整个 url,它的工作原理与上面的 .htaccess 相同;)

希望这个例子对你有帮助;)

再见

威尔克

PS: 更多详情:

  1. Manipulating the browser history
  2. History object
  3. History howto

关于javascript - HTML5 历史 API 演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10571734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com