Sometimes you want to add a button to a website that tells the browser to return to the previous page.
Yes, I know, the browser itself has such a function but there are sometimes cases where it makes sense to include a “back to previous page” button directly into the page.
- Within the “script” code, a function called “goBack()” is defined that utilizes the “window.history” object to go back one page in the browsing history when called.
- The “onclick” attribute on the button element is assigned the value of “goBack()”, so when the button is clicked, the function is executed, and the browser navigates back to the previous page in the history.
- Also, this code assumes that the previous page in the browsing history is the page that the user wants to go back to, which may not always be the case.