简体   繁体   中英

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

In my server side validation of a form, I am using 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); . 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); . 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).

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
         */
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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