Teachnique
      CourseRoadmaps
      Login

      OverviewPlacementSyntaxHello WorldConsole.log()CommentsVariableslet StatementConstantsData TypesType ConversionsStrict ModeReserved Keywords

      OperatorsArithmetic OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsAssignment OperatorsConditional Operatorstypeof OperatorNullish Coalescing OperatorDelete OperatorComma OperatorGrouping OperatorYield OperatorSpread OperatorExponentiation OperatorOperator Precedence

      If...ElseWhile LoopsFor LoopFor...in LoopFor...of LoopLoop ControlBreak StatementContinue StatementSwitch CaseUser Defined Iterators

      FunctionsFunction ExpressionsFunction ParametersDefault ParametersFunction() ConstructorFunction HoistingArrow FunctionsFunction InvocationFunction call() MethodFunction apply() MethodFunction bind() MethodClosuresVariable ScopeGlobal VariablesSmart Function Parameters

      NumberBooleanStringsArraysDateMathRegExpSymbolSetsWeakSetMapsWeakMapIterablesReflectTypedArrayTempate LiteralsTagged Templates

      Objects OverviewClassesObject PropertiesObject MethodsStatic MethodsDisplay ObjectsObject AccessorsObject ConstructorsNative PrototypesES5 Object MethodsEncapsulationInheritanceAbstractionPolymorphismDestructuring AssignmentObject DestructuringArray DestructuringNested DestructuringOptional ChainingGlobal ObjectMixinsProxies

      HistoryVersionsES5ES6ECMAScript 2016ECMAScript 2017ECMAScript 2018ECMAScript 2019ECMAScript 2020ECMAScript 2021ECMAScript 2022

      CookiesCookie AttributesDeleting Cookies

      Browser Object ModelWindow ObjectDocument ObjectScreen ObjectHistory ObjectNavigator ObjectLocation ObjectConsole Object

      Web APIHistory APIStorage APIForms APIWorker APIFetch APIGeolocation API

      EventsDOM Events

      Feedback

      Submit request if you have any questions.

      Course
      History Object

      JavaScript Tutorial

      This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.

      History Object

      Window History Object

      In JavaScript, the window 'history' object contains information about the browser's session history. It contains the array of visited URLs in the current session.
      The 'history' object is a property of the 'window' object. The history property can be accessed with or without referencing its owner object, i.e., window object.
      Using the 'history' object's method, you can go to the browser's session's previous, following, or particular URL.

      History Object Methods

      The window history object provides useful methods that allow us to navigate back and forth in the history list.
      Follow the syntax below to use the 'history' object in JavaScript.
      window.history.methodName();
      OR
      history.methodName();
      You may use the 'window' object to access the 'history' object.

      JavaScript History back() Method

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

      Syntax

      Follow the syntax below to use the history back() method.
      history.back();

      Example

      In the below code's output, you can click the 'go back' button to go to the previous URL. It works as a back button of the browser.
      <html>
      <body>
      <p> Click "Go Back" button to load previous URL </p>
      <button onclick="goback()"> Go Back </button>
      <p id = "output"> </p>
      <script>
      function goback() {
      history.back();
      document.getElementById("output").innerHTML +=
      "You will have gone to previous URL if it exists";
      }
      </script>
      </body>
      </html>

      JavaScript History forward() Method

      The forward() method of the history object takes you to the next URL.

      Syntax

      Follow the syntax below to use the forward() method.
      history.forward();

      Example

      In the below code, click the button to go to the next URL. It works as the forward button of the browser.
      <html>
      <body>
      <p> Click "Go Forward" button to load next URL</p>
      <button onclick = "goforward()"> Go Forward </button>
      <p id = "output"> </p>
      <script>
      function goforward() {
      history.forward();
      document.getElementById("output").innerHTML =
      "You will have forwarded to next URL if it exists."
      }
      </script>
      </body>
      </html>

      JavaScript History go() Method

      The go() method of the history object takes you to the specific URL of the browser's session.

      Syntax

      Follow the syntax below to use the go() method.
      history.go(count);
      In the above syntax, 'count' is a numeric value representing which page you want to visit.

      Example

      In the below code, we use the go() method to move to the 2nd previous page from the current web page.
      <html>
      <body>
      <p> Click the below button to load 2nd previous URL</p>
      <button onclick = "moveTo()"> Move at 2nd previous URL </button>
      <p id = "output"> </p>
      <script>
      function moveTo() {
      history.go(-2);
      document.getElementById("output").innerHTML =
      "You will have forwarded to 2nd previous URL if it exists.";
      }
      </script>
      </body>
      </html>

      Example

      In the below code, we use the go() method to move to the 2nd previous page from the current web page.
      
      <html>
      <body>
      <p> Click the below button to load 2nd next URL</p>
      <button onclick = "moveTo()"> Move at 2nd next URL </button>
      <p id = "output"> </p>
      <script>
      function moveTo() {
      history.go(2);
      document.getElementById("output").innerHTML =
      "You will have forwarded to 2nd next URL if it exists.";
      }
      </script>
      </body>
      </html>
      Following is the complete window history object reference including both properties and methods −

      History Object Property List

      The history object contains only the 'length' property.
      Property
      Description
      length
      It returns the object's length, representing the number of URLS present in the object.

      History Object Methods List

      The history object contains the below 3 methods.
      Method
      Description
      back()
      It takes you to the previous web page.
      forward()
      It takes you to the next web page.
      go()
      It can take you to a specific web page.