Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inconsistency with window.history.back()

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

enter image description here

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?

like image 602
Randy Minder Avatar asked Dec 15 '22 12:12

Randy Minder


1 Answers

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>
like image 56
Kevin B Avatar answered Jan 10 '23 04:01

Kevin B