简体   繁体   English

Javascript / jQuery:history.go(-1)有任何事件吗?

[英]Javascript / jQuery: Any event for history.go(-1)?

In my server side validation of a form, I am using history.go(-1); 在表单的服务器端验证中,我使用的是history.go(-1); to redirect back in case of validation error. 验证错误时重定向回。 This way form data remains there and user can fix data and resubmit the form. 这样,表单数据将保留在那里,用户可以修复数据并重新提交表单。

It works well until I use captcha. 直到我使用验证码,它才能正常工作。 If user submits wrong captcha, the server validation redirects him back using history.go(-1); 如果用户提交了错误的验证码,则服务器验证将使用history.go(-1);将其重定向回去history.go(-1); . This way, the page is not actually reloading and captcha image dosen't refresh. 这样,该页面实际上并没有重新加载,并且验证码图像也没有刷新。 So the user keeps submitting wrong captcha. 因此,用户不断提交错误的验证码。

So is there any way I can trigger an event or function every time page is redirected back via history.go(-1); 那么每次通过history.go(-1);将页面重定向回时,有什么方法可以触发事件或函数history.go(-1); . This way I can call a function which would reload captcha image. 这样,我可以调用一个函数来重新加载验证码图像。

I have to do it this way, because my form processing is being done on a separate server. 我必须这样做,因为我的表单处理是在单独的服务器上完成的。

Thanks. 谢谢。

Your solution here is to make the captcha image not cacheable. 您的解决方案是使验证码图片不可缓存。 You need to instruct your captcha script to send a response header with the image, either with a "expires" date in the past or a no cache one. 您需要指示验证码脚本发送带有图像的响应标头,其中包含过去的“过期”日期或没有缓存的日期。 That way, the browser will reload the captcha on every history.go(-1). 这样,浏览器将在每个history.go(-1)上重新加载验证码。

window.onpopstate是您所需要的。

I think youu need an history adapter: 我认为您需要一个历史适配器:

History.Adapter.bind (window, 'statechange', function () {});

this will trigger the funcion at each history state change. 这将在每次历史记录状态更改时触发功能。

var url = location.href;
window.onpopstate = function(event) {
    if(url != location.href) {
        /*Your Function here
         * I'm using this to load the contentBox when user goes back or forward in  
         * browserhistory -- perfect for sites with ajax
         * works on all browser but u need Jquery. (Safari can't use window.location.href)
         * for functions to add dynamic params or removing them just ask ;)
         * define the url var to prevent function to fire on reload
         */
    }
};

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM