Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7954119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:19:37+00:00 2026-06-04T03:19:37+00:00

I’ve been reading about the HTML5 history API and so far, I haven’t found

  • 0

I’ve been reading about the HTML5 history API and so far, I haven’t found a simple working demo that shows the mechanics with code.

Here is a working jsfiddle: 4 buttons and 4 divs. When the user presses a button, it shows the corresponding panel.

What I’m looking to do is:

1) rewrite the URL so that when the user is on panel 4 the url ends with /Panel4
2) make the back button and forward button work with the history API.

I know there’s the history.js plug-in but I want to understand how the API works in its simplest form.

Hopefully, the jsfiddle will help others who’ll come to this page looking for a code demo.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:19:38+00:00Added an answer on June 4, 2026 at 3:19 am

    Ok, I created what I think is the SIMPLEST form of history API demo.

    It can’t work in jsfiddle because it needs to run in its own window. It will however work if you copy-paste the code in notepad, add a reference to jquery where indicated, and save it on your desktop as an html file. Of course, it doesn’t work in IE, but we all know that.
    I’ve put in two versions: the one that works without the URL rewrite component (it’ll work from your desktop) and I’ve also commented out the version where you can manipulate the URL. For the latter, you need to run it from a server, either remote or local.

    I’ve struggled to get it working across all browsers because Chrome, Safari and Firefox work differently! Here’s the code:

        <html>
        <head>
        <style type="text/css">
    
        .Panel{
           width:200px;
           height:100px;
           background:red;
           display:none;
           color:white;
           padding:20px 20px;}
    
        .ChangeButton{
           margin:10px 10px;
           float:left;}   
    
        </style>
    
       // add reference to jquery.js file here 
       // <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>        
    
        <script type="text/javascript">
    
        var TheURL; // if you don't need URL rewrite, then we're always going to 
                    // show the same URL. Remove this line if you want URL rewrite.
    
        var MyDivs = { // this object stores the name and the URL of each possible state
           ShowPanel1: {panelID:'Panel1', DisplayURL:'/panel1'},
           ShowPanel2: {panelID:'Panel2', DisplayURL:'/panel2'},
           ShowPanel3: {panelID:'Panel3', DisplayURL:'/panel3'},
           ShowPanel4: {panelID:'Panel4', DisplayURL:'/panel4'},
        };
    
        $(document).ready(function () {
    
        TheURL = document.URL; // You can remove this line if you're doing
                               // URL rewrite
    
        window.addEventListener('popstate', function (event) {
    
           //cross-browser nightmare here!!!!!
           var HistoryState = history.state;
    
           if (HistoryState === null || HistoryState === undefined) {
                HistoryState = event.state; }
    
           if (HistoryState === null || HistoryState === undefined) {
                HistoryState = window.event.state; }
    
           SwitchPanel(HistoryState);
        });
    
        $('.ChangeButton').click(function () {
               DoChange(parseInt($(this).attr('id').charAt(6), 10)); });
    
        DoChange(1);
    
        });
    
        function DoChange(ButtonID) {
    
           switch (ButtonID) {
    
           // here's the 2-version option: 
           // toggle the commented and uncommented history.pushState
           // lines to see the change to the URL in action
           case 1:
               SwitchPanel(MyDivs.ShowPanel1.panelID);
               history.pushState(MyDivs.ShowPanel1.panelID, "", TheURL);
               // history.pushState(MyDivs.ShowPanel1.panelID, "", MyDivs.ShowPanel1.DisplayURL);
               break;
           case 2:
               SwitchPanel(MyDivs.ShowPanel2.panelID);
               history.pushState(MyDivs.ShowPanel2.panelID, "", TheURL);
               // history.pushState(MyDivs.ShowPanel2.panelID, "", MyDivs.ShowPanel2.DisplayURL);
               break;
           case 3:
               SwitchPanel(MyDivs.ShowPanel3.panelID);
               history.pushState(MyDivs.ShowPanel3.panelID, "", TheURL);
               // history.pushState(MyDivs.ShowPanel3.panelID, "", MyDivs.ShowPanel3.DisplayURL);
               break;
           case 4:
               SwitchPanel(MyDivs.ShowPanel4.panelID);
               history.pushState(MyDivs.ShowPanel4.panelID, "", TheURL);
               // history.pushState(MyDivs.ShowPanel4.panelID, "", MyDivs.ShowPanel4.DisplayURL);
               break;
           }
        }
    
        function SwitchPanel(PanelID) {
    
           if (PanelID === null) {return false;}
    
           $('.Panel').hide();
           $('#' + PanelID).fadeIn('medium');
        }
    
        </script>
        </head>
    
        <body>
    
        <input type="button" id="Button1" class="ChangeButton" value="panel 1" />
        <input type="button" id="Button2" class="ChangeButton" value="panel 2" />
        <input type="button" id="Button3" class="ChangeButton" value="panel 3" />
        <input type="button" id="Button4" class="ChangeButton" value="panel 4" />
    
        <div id="PanelContainer" style="clear:both;">
    
           <div class="Panel" id="Panel1">panel 1</div>
           <div class="Panel" id="Panel2">panel 2</div>
           <div class="Panel" id="Panel3">panel 3</div>
           <div class="Panel" id="Panel4">panel 4</div>
    
        </div>
    
        </body>
        </html>
    

    Upvote if it works for you.

    Enjoy!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.