Displays the number of pages that the users browser has displayed in its session

CK CK 3788 
Created at
Updated at  
667 0 0

The `history` Object
The `history` object in JavaScript provides a mechanism to interact with the browser's browsing history. It's a global object accessible within any JavaScript code running within a web page.


Key Properties and Methods

  • `history.length`: This property returns the number of entries in the browser's history stack (the number of pages visited).
  • `history.back()`: Navigates back to the previous page in the history stack.
  • `history.forward()`: Navigates forward to the next page in the history stack, if any.
  • `history.go(n)`: Navigates to a specific page in the history stack. `n` can be a positive integer to go forward, a negative integer to go back, or 0 to reload the current page.
  • `history.pushState(state, title, url)`: Adds a new entry to the history stack without actually loading a new page. The arguments let you specify the state, title, and URL for the new entry.
  • `history.replaceState(state, title, url)`: Replaces the current history entry with a new one. The arguments are the same as `pushState`.

 

Below example counts the number of pages navigated by browser.

<script language="javascript">
<!--
document.writeln('');
var times=history.length;
document.write('<B>You've been to '+times+' page(s) in this browser session.</b>');
//-->
</script>

 

Important Considerations

  • Security: Browsers have implemented security measures to prevent malicious scripts from manipulating the history object in ways that could harm users. For instance, you can't directly access the URLs stored in the history object.
  • Cross-Origin: JavaScript code can only manipulate the history object of the same origin (domain, protocol, port) as the current page.
  • `history.pushState` and `history.replaceState` don't directly trigger page reloads. They merely add or modify entries in the history stack. To actually load the new URL, you'll need to manually navigate to it using `history.go()`.
  • Browser Compatibility: The `history` object and its methods are supported by modern browsers, but older ones might not have complete support.

 

Example

// Navigate back to the previous page
history.back();
// Navigate forward two pages
history.go(2);
// Add a new history entry without loading a new page
history.pushState({ page: 'about' }, 'About Page', '/about');
// Replace the current history entry
history.replaceState({ page: 'home' }, 'Home Page', '/'); 

Let me know if you have any specific scenarios or questions about the `history` object in mind! 
 

Tags HTML Javascript history.length loadCookie Facebook X
Comments 0
Similar posts
  1. Greeting For The Time Of Day
    393
  2. Address Book
    576
  3. Access Granted
    586
  4. Cookie enabled browser checking in Javascript
    342
  5. Cookie Redirect
    496
  6. New content notifier in Javascript
    597
  7. Let popup once for event dialog or something like that in Javascript
    425
  8. Save And Restore Form Cookies in Javascript
    335
  9. Basic Date Display
    511
  10. Javascript implemented Calendar
    421
  11. Javascript based Alarm Clock
    666
  12. How to get date time in Javascript ?
    498
  13. Hiding HTML on the browser
    461
  14. Getting WebSearchers On Your Site
    373
  15. Javascript Mouse Event Handler Example
    403
  16. Select And Deselect All Checkboxes By Javascript
    632
  17. How to record visitor's name by Cookie
    411
  18. Javascript & Cookie based Visitor Application
    435
  19. Cross Browser Event Management Functions Using JavaScript
    611
  20. Printing A Page
    379
  21. Example Of Creating Objects
    403
  22. Linking Drop Downs
    576
  23. Roman Numerals From Integers
    511
  24. Get A Print Dialog Box By Clicking A Button
    818
  25. Keyboard event detection (Key Tracker)
    1,087
  26. IsIn For Arrays
    568
  27. Confirm The Link Clicked
    631
  28. Enlarge Image
    514
  29. Dual Scroller
    332
  30. Cursor Changer
    383
  1. Cursor Changer
    383
  2. Dual Scroller
    332
  3. Enlarge Image
    514
  4. Confirm The Link Clicked
    631
  5. Send An Email Using A Bean
    1,015
  6. Select And Deselect All Checkboxes By Javascript
    632
  7. Javascript Mouse Event Handler Example
    403
  8. Getting WebSearchers On Your Site
    373
  9. Hiding HTML on the browser
    461
  10. jQuery and Function Chaining
    536
  11. How to get date time in Javascript ?
    498
  12. Javascript based Alarm Clock
    666
  13. Javascript implemented Calendar
    421
  14. Basic Date Display
    511
  15. Address Book
    576
  16. Greeting For The Time Of Day
    393
  17. Access Granted
    586
  18. Cookie enabled browser checking in Javascript
    342
  19. Cookie Redirect
    496
  20. New content notifier in Javascript
    597
  21. Let popup once for event dialog or something like that in Javascript
    425
  22. Save And Restore Form Cookies in Javascript
    335
  23. How to record visitor's name by Cookie
    411
  24. Javascript & Cookie based Visitor Application
    435
  25. Cross Browser Event Management Functions Using JavaScript
    611
  26. Printing A Page
    379
  27. Example Of Creating Objects
    403
  28. Linking Drop Downs
    576
  29. Roman Numerals From Integers
    511
  30. Get A Print Dialog Box By Clicking A Button
    818
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    40
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,613
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    244
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    217
  30. Understanding Data Lineage
    260