How to get the different in number between two browser history

12 Views Asked by At

I have some browser history input, for example let's assume this is what the history looks like when clicked

http://example.com/index.php
http://example.com/profile.php
http://example.com/comments.php
http://example.com/contact.php

Now the difference between the index page and the contact page is 3, how can I calculate it

var currentPage = "index.php";
var historyPage = "contact.php";
var pageDiff = // historyPage - currentPage
1

There are 1 best solutions below

2
On

If you want to know the distance between two items in an array, you can use indexOf. I'm assuming your browser history is in an array, and not just one big text blob.

Something like:

var history = [ 
    'http://example.com/index.php',
    'http://example.com/profile.php',
    'http://example.com/comments.php',
    'http://example.com/contact.php', 
]  
var pageDiff = history.indexOf("http://example.com/contact.php")-history.indexOf("http://example.com/index.php");