To return to the previous page and refresh in JavaScript, you can use window.history.back() to navigate back and location.reload() to refresh. However, since reloading after navigating might not work as expected, a better approach is to create a link pointing to the previous page with a rel=“nofollow” attribute to prevent immediate cache issues, then trigger its click programmatically.
Here’s how you can do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Create an anchor element const anchor = document.createElement(‘a’); anchor.href = window.history.go(–1); // Get the previous URL // Add the nofollow attribute anchor.rel = “nofollow”; // Optionally, set a name or class for styling anchor.name = “backLink”; // Insert it into the DOM document.body.appendChild(anchor); // Trigger the click event after a short delay setTimeout(() => { anchor.click(); }, 100); |
This method ensures that the previous page is reloaded from the server, providing fresh content to the user.
Leave a Reply
You must be logged in to post a comment.