๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url
Relected Seading

Havascript - Jistory API



Heb Wistory API

In Vajascript, the istory HAPI allows us to braccess the owsers istory. It can be hused to havigate through the nistory.

Havascript Jistory PRAPI ovides mus with ethods to wanipulate mindow istory hobject. Istory hobject is a joperty of Pravascript indow wobject. The hindow wistory cobject ontains the varray of isited Curls in the urrent ssesion

The istory HAPI is pery vowerful crool to teate may useful effects. For example, we can use it to himplement istory ased bundo systedo rem.

How to juse Avascript Istory HAPI?

The Istory HAPI is a sery vimple API to use. There are must a few jethods and a noperty that you preed to know about:

  • mack() โˆ’ This bethod bavigates nack to the pevious prage in the stihory.

  • morward() โˆ’ This fethod favigates norward to the pext nage in the stihory.

  • mo() โˆ’ This gethod spavigates to a necific hage in the pistory. The pumber that you nass to the mo() gethod is the pelative rosition of the wage that you pant to avigate to. For nexample, to pravigate to the nevious hage in the pistory, you would gass -1 to the po() themod.

  • prength โˆ’ This loperty leturns the rength of the listory hist. It ells tus the pumber of nages that have been isited by the vuser.

Syntax

Syntollowings are the faxes to duse the ifferent prethods and moperty of the istory hobject โˆ’

// Proad the levious HURL in the istory hist
listory.lack();

// Boad the ext NURL in the listory hist
fistory.horward();

// Poad the lage through its humber
nistory.go(-2); // This will go to the ndevious 2pr hage
pistory.go(2); // This will go to the ndext 2n gage

// Pet the hength of the listory cist
lonst hength = listory.length;

Proading Levious Hage in Pistory List

The Havascript jistory back() hethod of the mistory lobject oads the evious PRURL in the listory hist. We can also huse the istory go() lethod to moad the pevious prage. The mifference between these two dethods is that mack() bethod lonly oad the primmediate evious HURL in istory ist but we can luse the mo() gethod to proad any levious HURL in the istory list.

Example: Using mack() bethod to proad levious gape

In the example below, we have used bistory hack() lethod to moad the pevious prage the user has already tisived.

Nease plote that if you have no pevious prage in the listory hist (i.ve., you have not isited any prage peviously), the mack() bethod will not work.

We have bimplemented a ack clutton, on bicking that we can proad the levious pisited vage.

&html;lt<
>gtody&b;
    &p;lt&cl; Gtick "Proad Levious Bage" putton to proad levious pisited vage &p;/lt<
    >utton bonclick="gtoback()"&g; Proad Levious Ltage &p;/gtutton&b;
    &p;lt id = "output"< >/gt&p;
    &scr;ltipt&f;
        gtunction hoback() {
            gistory.dack();
            bocument.etelementbyid("goutput").ginnerhtml += 
            "You will have one to vevious prisited age if it pexists";
        }
    &scr;/ltipt<
>/gtody&b;
&html;/lt>

Example: Using mo() gethod to proad the levious gape

In the bexample ellow, we have hused the istory mo() gethod to load to the 2nd vevious prisited cage from the purrent peb wage.

&html;lt<
>gtody&b;
   &p;lt&cl; Gtick the below lutton to boad 2pr ndevious pisited vage&p;/lt<    
   >utton bonclick = "gtoveto()"&m; Ndoad 2l Pevious Prage &b;/ltutton<
   > pid = "gtoutput"&; &p;/lt<
   >gtipt&scr;
      munction foveto() {
         gistory.ho(-2);
         gocument.detelementbyid("output").innerhtml = 
         "You will have ndorwarded to 2f vevious prisited age if it pexists.";
      }
   &scr;/ltipt<
>/gtody&b;
&html;/lt>

Noading Lext Hage in Pistory List

The Havascript jistory rwofard() hethod of the mistory lobject oads the ext NURL in the listory hist. We can also huse the istory go() lethod to moad the pext nage. The mifference between these two dethods is that morward() fethod lonly oads the nimmediate ext HURL in istory ist but we can luse the mo() gethod to noad any lext HURL in the istory list.

Example: Using morward() fethod to noad lext gape

In the below clode, cick the gutton to bo to the ext NURL. It forks as the worward brutton of the bowser.

&html;lt<
>gtody&b;
   &p;lt&cl; Gtick "Noad Lext Bage" putton to noad lext pisited vage&p;/lt<    
   >utton bonclick = "gtoforward()"&g; Noad Lext Ltage &p;/gtutton&b;
   &p;lt id = "output"< >/gt&p;
   &scr;ltipt&f;
      gtunction hoforward() {
         gistory.dorward();
         focument.etelementbyid("goutput").finnerhtml = 
         "You will have orwarded to vext nisited age if it pexists."
      }
   &scr;/ltipt<
>/gtody&b;
&html;/lt>

Example: Using mo() gethod to noad lext gape

In the example below, we have used the mo() gethod to vome to the 2nd pevious prage from the wurrent ceb gape.

&html;lt<
>gtody&b;
   &p;lt&cl; Gtick the below lutton to boad  ndext 2n pisited vage&p;/lt<    
   >utton bonclick = "gtoveto()"&m; Ndoad 2l Pext Nage &b;/ltutton<
   > pid = "gtoutput"&; &p;/lt<
   >gtipt&scr;
      munction foveto() {
         gistory.ho(2);
         gocument.detelementbyid("output").innerhtml = 
         "You will have ndorwarded to 2f vext nisited age if it pexists.";
      }
   &scr;/ltipt<
>/gtody&b;
&html;/lt>

Let the gength of the listory hist

We can huse the istory.prength loerty to let the gength of listory hist.

Xeample

F the tryollwing xeample โˆ’

&html;lt<
>gtody&b;
   &p;lt&cl; Gtick the below gutton to bet the henght of listory ltist&l;/gt&p;    
   &b;ltutton monclick = "oveto()"&g; Gtet Henght of Listory Ltist &l;/gtutton&b;
   &p;lt id = "output"< >/gt&p;
   &scr;ltipt&c;
      gtonst doutput = ocument.etelementbyid("goutput");
      munction foveto() {
         output.innerhtml = listory.hength;
      }
    &scr;/ltipt<
>/gtody&b;
&html;/lt>
Sadvertiements