按下后退按钮后,页面加载时jQuery(或常规Javascript)执行功能

时间:2021-01-13 01:19:08

I've seen this on a lot of websites, but I can't figure out exactly how to do it:

我在很多网站上看过这个,但我无法弄明白该怎么做:

When a function is executed, the URL changes to whatever.com/page/#something and then when that url is accessed, the function is executed again. I know how to make that work when the page is first loaded, but I can't figure out how to make the function occur again when the "back" button is pressed.

执行函数时,URL将更改为whatever.com/page/#something,然后在访问该URL时,将再次执行该函数。我知道如何在首次加载页面时使其工作,但我无法弄清楚当按下“后退”按钮时如何再次发生该功能。

(e.g. if I have a tabbed system set up, I start on site.com/#tab1, go to site.com/#tab2, and click the back button so that tab 1 shows up on the page again)

(例如,如果我设置了选项卡式系统,请从site.com/#tab1开始,转到site.com/#tab2,然后单击后退按钮,以便选项卡1再次显示在页面上)

How does that work?

这是如何运作的?

1 个解决方案

#1


2  

You'll want to look into history management in Javascript. If you aren't using the browser's built-in functionality to maintain history - which is how the back and forward buttons work - you'll need to do a bit of work yourself.

您将要在Javascript中查看历史记录管理。如果您没有使用浏览器的内置功能来维护历史记录 - 后退和前进按钮的工作方式 - 您需要自己做一些工作。

jQuery has several plugin options to help you out. hashchange for example, looks to provide the event you're looking for with an onhashchange event added for browsers that don't have it implemented yet.

jQuery有几个插件选项可以帮助你。例如,hashchange看起来提供了你正在寻找的事件,为尚未实现它的浏览器添加了一个onhashchange事件。

#1


2  

You'll want to look into history management in Javascript. If you aren't using the browser's built-in functionality to maintain history - which is how the back and forward buttons work - you'll need to do a bit of work yourself.

您将要在Javascript中查看历史记录管理。如果您没有使用浏览器的内置功能来维护历史记录 - 后退和前进按钮的工作方式 - 您需要自己做一些工作。

jQuery has several plugin options to help you out. hashchange for example, looks to provide the event you're looking for with an onhashchange event added for browsers that don't have it implemented yet.

jQuery有几个插件选项可以帮助你。例如,hashchange看起来提供了你正在寻找的事件,为尚未实现它的浏览器添加了一个onhashchange事件。