JavaScript location.href does not add entry to browser history

There are three pages: A, B and C. A has a tag whose href redirects to B. B has JavaScript code and redirects to C. When in C, I click the browser back button, the browser redirects to A. Why not B? Thank you in advance.

Test Links Page A: http://o17o2o.com:8000/article-href.html

Page A

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <title>Test</title> </head> <body> <a href="/href-redirect.html">click me</a> </body> </html> 

Page B

 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <title>waiting</title> </head> <body> Page B <script type="text/javascript"> window.location.href = '/original.html'; </script> </body> </html> 
+5
source share
1 answer

location.href redirection will be added to the browser history only if it is initiated by the user. Here you call it on the onload event, so it cannot be added to the history

History.pushState user to insert into browser history

+1
source

Source: https://habr.com/ru/post/1259586/


All Articles