简体   繁体   English

在Javascript中,最好是JQuery,当用户点击浏览器中的“后退”按钮时,如何在URL中添加内容?

[英]In Javascript, preferably JQuery, how do I add something to the URL when the user clicks “back” button in the browser?

When the browser clicks "back" button, I want to append "&page=3" to the URL. 当浏览器点击“后退”按钮时,我想在URL上附加“&page = 3”。 How do I bind it, and then do this? 我如何绑定它,然后这样做?

Edit: I want this very simple. 编辑:我希望这很简单。

BIND the event{
window.location(that-url + "&page=3")
}

Can it be done that simply? 这可以简单地完成吗?

替代文字 It sounds like you're trying to create a history plugin. 听起来你正在尝试创建一个历史插件。

Have you tried using using the mikage history plugin ? 您是否尝试过使用mikage历史插件

I wouldn't recommend changing the URL when they navigate away from the current page (which is what the back button does), because you immediately erase the forward history (thus breaking the forward button). 我不建议在离开当前页面时(这是后退按钮的操作)更改URL,因为您会立即删除前进历史记录(从而中断前进按钮)。 When trying to handle the back button with pagination and javascript/ajax it is more typical to use the browser hash to pass parameters. 当尝试使用分页和javascript / ajax处理后退按钮时,更典型的是使用浏览器哈希来传递参数。 The JavaScript namespace doesn't get cleared when the forward and backward buttons are used and the hash is updated according to what navigation was used. 使用前进和后退按钮时,不会清除JavaScript命名空间,并根据使用的导航更新哈希值。 These history plugins have a couple of methods to detect when navigation is used (as the doc load event doesn't fire). 这些历史插件有两种方法可以检测何时使用导航(因为doc load事件不会触发)。

So beware, writing a history plugin isn't straightforward because of the way browsers fail to consistently handle hash property of the location object (part of the window object). 所以要注意,编写历史插件并不简单,因为浏览器无法一致地处理位置对象(窗口对象的一部分)的哈希属性。 You will definitely want to look at what others have done. 你肯定想看看别人做了什么。

We use the window.location.hash to handle the history in our app. 我们使用window.location.hash来处理我们应用中的历史记录。
I guess it works well in single page apps and is very simple. 我想它在单页应用程序中运行良好,非常简单。

For multiple pages app, I don't think it's a good idea to try to control and change the natural page history of the browser. 对于多页面应用程序,我认为尝试控制和更改浏览器的自然页面历史记录并不是一个好主意。

When the user clicks "back" or "next", the hash key gets the previous or next value. 当用户单击“后退”或“下一步”时,散列键将获取上一个或下一个值。
Because of IE7 you need to use a polling technique (but it is ok in all browsers), with a setInterval(...) and a fast function that checks for instance every 300ms if the hash has changed. 由于IE7,您需要使用轮询技术(但在所有浏览器中都可以),使用setInterval(...)和快速函数,如果哈希值已更改,则每隔300毫秒检查一次。

Then, if a change occurs, act accordingly. 然后,如果发生变化,则相应地采取行动。
ie: call the server and refresh some areas in the page. 即:调用服务器并刷新页面中的某些区域。

It works very well, and does not kill at all the responsiveness of the application. 它运行良好,并不会杀死应用程序的所有响应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户尝试按“后退”按钮或使用Firefox时,如何添加警报? (JavaScript) - How do I add an alert when the user tries to press the back button or is using firefox? (Javascript) Javascript:当用户在浏览器中单击BACK按钮时,使div保持display ='block'状态 - Javascript: Making divs hold state of display='block' when user clicks BACK button in browser 如何查找用户是单击浏览器后退按钮还是“刷新”按钮 - How to find whether the user clicks browser back button or Refresh button 当用户单击浏览器 React Js 中的后退按钮时关闭模式 - Close a modal when user clicks back button in browser React Js 当用户单击浏览器的后退按钮时隐藏div - Hide div when user clicks browser's back button 用户单击浏览器后退按钮时提交表单 - submit form when user clicks browser back button 用户单击浏览器的“后退”按钮时清除文本字段/文本框 - Clearing textfields/textboxes when user clicks the browser's Back button 在用户单击javascript / jquery中的按钮之前,如何防止元素加载? - How can I prevent an element to load until such time when the user clicks a button in javascript/jquery? 如果用户单击浏览器后退按钮,如何清除 Django 中的 self.add_error? - How To Clear self.add_error in Django if user clicks on browser back button? 当用户点击按钮时,如何让html运行javascript函数? - How do I make html run a javascript function when a user clicks a button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM