简体   繁体   中英

Inconsistency with window.history.back()

I have the following code in an MVC view that I'm using to test window.history.back()

在此输入图像描述

If the user clicks the link (highlighted in red) within the paragraph tag, window.history.back() executes as I would expect. It takes me back to the prior page with state maintained on that page. However, if the user clicks the button, I don't get the same behavior. The current view is reloaded, or at least an attempt is made to reload the current page. But it fails. And it doesn't matter if the jQuery is executed, or I put the window.history.back() call within the Button onClick, the same thing happens.

A piece of information which might be helpful. The button is inside an HTML.BeginForm and the line in the paragraph tag is not.

Anyone know why this is happening?

The browser is probably interpreting the button as a submit button and submitting the form, thus causing a page refresh. Adding type="button" will prevent that.

<button type="button">Cancel</button>
$('#cancelButton').click(function(e){
    e.preventDefault();

    /* ... code ... */
});

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