Yay*_*295 6 javascript html5 browser-history html5-history
我在Stack Overflow上发现了很多关于这个问题的问题,但是他们都对某些部分非常具体.我确实找到了这个问题,其答案提供了一些很好的参考,但它们并没有真正解释它是如何工作的,它们的例子几乎没有做任何事情.我想更多地了解它是如何一起工作的,我想使用vanilla JavaScript.
(此外,其他问题的许多答案都已有数年之久了.)
Yay*_*295 30
首先,您可以删除该window部件.只要history能正常工作.但在我们开始讨论一切如何协同工作之前,我们需要知道我们可以使用什么.
加载网页时会触发此事件.有两种情况会触发此事件:
当历史状态之间进行切换此事件你已设置.您的浏览器会在正常浏览期间自动设置历史记录状态(为空),但导航到这些状态或从这些状态导航不会触发此事件.
每当您的网页被卸载时,此事件都会触发.有两种情况会触发此事件:
历史界面包含五个函数(如下所述),两个只读对象(此处描述),并且有点像链接列表.历史对象的每个"链接"中包含的两个对象是:
null默认的.您可以分别通过调用history.length和访问它们history.state,但history.state只能用于获取当前的历史状态.
此功能与按下浏览器中的后退或前进按钮的功能相同,增加的功能是能够准确指定您想要的距离.例如,history.go(3)与按下前进按钮三次具有相同的效果,而不实际在开始和结束位置之间加载页面.负值同样会使您在历史中向后移动.history.go(0),history.go()甚至history.go(NaN)与刷新页面具有相同的效果(这不会触发popstate事件).如果您无法向前/向后移动指定的范围,则该功能将不执行任何操作.
此功能与浏览器中的后退按钮功能相同.它相当于history.go(-1).如果它不能返回,该功能将什么都不做.
此功能与浏览器中的前进按钮功能相同.它相当于history.go(1).如果它不能继续,该功能将什么都不做.
此函数替换当前的历史状态.它需要三个参数,尽管最后一个是可选的.论点是:
history.state供以后检索.这是一个深层复制,因此如果您以后修改原始对象,它将不会更改已保存的状态.您也可以将其设置为null,但如果您不打算使用它,则根本没有用处history.popstate而不是load或unload.更改其URL后刷新页面将加载由URL指定的页面而不是您之前使用的页面.此功能可用于在当前状态下提供指向页面的链接,但我建议仅更改查询字符串而不是完整URL.如果未使用此参数,则URL不会更改.该功能的工作方式相同history.replaceState,但它提出的新的状态后,目前的状态,而不是替换当前状态.之前可以访问的所有历史状态都将被forward丢弃,新状态将成为当前状态.
历史界面非常有用,允许用户在浏览器中浏览动态生成的内容,而无需重新加载整个页面,但您需要注意用户可能会影响历史状态的所有可能事项.
history甚至是JavaScript,您的页面是否会正确显示?back/forward返回到您的网页
back/forward从刷新页面
注意,没有办法删除已保存的状态(除了pushState()上面提到的特定情况).您只能将其替换为新内容.
由于这开始有点罗嗦,让我们用一些代码完成它.
// This function is called when the page is first loaded, when the page is refreshed,
// and when returning to the page from another page using back/forward.
// Navigating to a different page with history.pushState and then going back
// will not trigger this event as the page is not actually reloaded.
window.onload = function() {
// You can distinguish a page load from a reload by checking performance.navigation.type.
if (window.performance && window.PerformanceNavigation) {
let type = performance.navigation.type;
if (type == PerformanceNavigation.TYPE_NAVIGATE) {
// The page was loaded.
} else if (type == PerformanceNavigation.TYPE_RELOAD) {
// The page was reloaded.
} else if (type == PerformanceNavigation.TYPE_BACK_FORWARD) {
// The page was navigated to by going back or forward,
// though *not* from a history state you have set.
}
}
// Remember that the browser automatically sets the state to null on the
// first visit, so if you check for this and find it to be null, you know
// that the user hasn't been here yet.
if (history.state == null) {
// Do stuff on first load.
} else {
// Do stuff on refresh or on returning to this page from another page
// using back/forward. You may want to make the window.onpopstate function
// below a named function, and just call that function here.
}
// You can of course have code execute in all three cases. It would go here.
// You may also wish to set the history state at this time. This could go in the
// if..else statement above if you only want to replace the state in certain
// circumstances. One reason for setting the state right away would be if the user
// navigates to your page via a deep link.
let state = ...; // There might not be much to set at this point since the page was
// just loaded, but if your page gets random content, or time-
// dependent content, you may want to save something here so it can
// be retrieved again later.
let title = ...; // Since this isn't actually used by your browser yet, you can put
// anything you want here, though I would recommend setting it to
// null or to document.title for when browsers start actually doing
// something with it.
let URL = ...; // You probably don't want to change the URL just yet since the page
// has only just been loaded, in which case you shouldn't use this
// variable. One reason you might want to change the URL is if the
// user navigated to this page with a query string in the URL. After
// reading the query string, you can remove it by setting this
// variable to: location.origin + location.pathname
history.replaceState(state, title, URL); // Since the page has just been loaded, you
// don't want to push a new state; you should
// just replace the current state.
}
// This function is called when navigating between states that you have set.
// Since the purpose of `history` is to allow dynamic content changes without
// reloading the page (ie contacting the server), the code in this function
// should be fairly simple. Just things like replacing text content and images.
window.onpopstate = function() {
// Do things with history.state here.
}
// This function is called right before the page is refreshed, and right
// before leaving the page (not counting history.replaceState). This is
// your last chance to set the page's history state before leaving.
window.onunload = function() {
// Finalize the history state here.
}
Run Code Online (Sandbox Code Playgroud)
请注意,我从未打过电话history.pushState.这是因为history.pushState不应该在这些函数中的任何地方调用它.它应该由实际以某种方式更改页面的函数调用,您希望用户能够使用后退按钮进行撤消.
总而言之,通用设置可能会像这样工作:
if (history.state == null)的window.onload功能.
history.pushState在重要事件发生时调用,使用后退按钮可以撤消.popstate触发事件时,请使用您设置的历史记录状态将页面返回到先前的状态.
unload在用户离开页面之前使用事件来完成历史状态.| 归档时间: |
|
| 查看次数: |
11441 次 |
| 最近记录: |