Create Browser History
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Question
Every modern browser has implemented a logic for keeping track of the users' browsing history. Usually, it keeps track of the websites visited, and their order, so the user can be able to go back and forth between their recently visited websites.
Can you implement a simplified version of a browser history tracker, that will keep track of the order of visited pages (URLs), and will implement logic for visiting the previous, current and following websites?
It should work like this:
We have an empty browser history
[]
Then we visit three websites in the following order: First we visit website A, then B, and then C. Then our browsing history would look like this, having website C as the page we are currently visiting:
js
[ A, B, C]
↑
If we "go back", or call the method back(), our current website would be B, our previous would be A, and the next one will be C.
[ A, B, C]
↑
Thus, by "going forward", or calling the method forward(), we would go to the website C again.

Solution
Browser History Tracker: A Simplified Version
Understanding how browser history works can be intriguing. Browsers are quite efficient at keeping a tab of our actions. Today, we'll create a simplified model of a browser history tracker. This tracker will maintain the sequence of visited pages (URLs) and allow us to navigate forward and backward in history. 🌐
Class Structure and Initial Setup
Let's begin by laying down the groundwork for our Browser History class. We'll need two main properties:
- History Array: To keep track of all the visited URLs.
- Pointer: An integer that points to the current URL we're viewing in the history array.
Here's the initial class setup with a constructor:
class BrowserHistory {
constructor(url) {
this.history = [];
this.pointer = -1;
if (url !== undefined) {
this.history.push(url);
}
}
}
What's happening here?
- The constructor initializes an empty
historyarray and sets thepointerto -1. - If a URL is provided during the instantiation, it gets added to the
history.
Putting It All Together
Putting It All Together
Here's the full BrowserHistory class with all its glory:
class BrowserHistory {
constructor(url) {
this.history = [];
this.pointer = 0;
if (url !== undefined) {
this.history.push(url);
}
}
visit(url) {
this.history.length = this.pointer + 1;
this.history.push(url);
this.pointer++;
}
back() {
this.pointer = Math.max(0, --this.pointer);
}
forward() {
this.pointer = Math.min(this.history.length - 1, ++this.pointer);
}
}
And there you have it—a simplified browser history tracker! This class now allows you to maintain a list of visited URLs and navigate through them just like a real browser.
One Pager Cheat Sheet
One Pager Cheat Sheet
- You can create a simplified version of a browser history tracker that will keep track of the order of visited pages (URLs) and implement logic for visiting the previous, current and following websites.
- The
BrowserHistoryclass should include ahistoryarray, apointerproperty and methods for visiting, going back, and going forward to the previously and next visited website URLs.