Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript location.href doesn't add record 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 browser back button, 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>
like image 862
Yiqi Lee Avatar asked Nov 11 '16 09:11

Yiqi Lee


1 Answers

location.href redirect will only add to browser history if it is from user initiated action. Here you are calling it on onload event so it wont be added to history

user History.pushState to insert in browser history

like image 102
Sameer Avatar answered Oct 21 '22 21:10

Sameer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!