How To Use Window History Objects With Java Script

admin_img Posted By Bajarangi soft , Posted On 09-10-2020

In Java Script we can use window history object it will contains the browsers history.so today we discuss how to use window history objects

How To Use Window History Objects With Java Script

Window History

The window.history object can be written without the window prefix.

To protect the privacy of the users, there are limitations to how JavaScript can access this object.

Some methods:

  • history.back() - same as clicking back in the browser

  • history.forward() - same as clicking forward in the browser


Window History Back

The history.back() method loads the previous URL in the history list.

This is the same as clicking the Back button in the browser.

Example(1)

<input class="btn btn-success" type="button" value="Back" onclick="goBack()">

<script>
    function goForward() {
        window.history.forward()
    }
</script>

In above example Created a back button on a page.

Example(2)

<input class="btn btn-success" type="button" value="Forward" onclick="goForward()">
<script>
    function goBack() {
        window.history.back()
    }
</script>

In above example created a forward button on a page.

Complete Code For Using  Window History Objects With Java Script
<!DOCTYPE html>
<html>
<head>
    <title>How To Use Window History Objects With Java Script</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <div class="text-center">
        <h1 id="color" style="color: tomato">How To Use Window History Objects With Java Script</h1>
    </div>

    <div class="well">
        <input class="btn btn-success" type="button" value="Back" onclick="goBack()">
        <input class="btn btn-success" type="button" value="Forward" onclick="goForward()">
        <script>
            function goForward() {
                window.history.forward()
            }
            function goBack() {
                window.history.back()
            }
        </script>
    </div>
</div>
</body>
</html>

 

Related Post