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 8829709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:53:48+00:00 2026-06-14T07:53:48+00:00

I was able to put together a simplified complete History.js example using three links

  • 0

I was able to put together a simplified complete History.js example using three links to load fragments of content from a full page without updating the page and while updating the browser history.

Here are the relevent code snippets – a complete working example is here http://jsfiddle.net/PT7qx/show

<a href="/page-header">Page Header</a>
<a href="/login-form">Login Form</a>
<a href="/nothing">Nothing</a>

<script>
var History = window.History;
if (!History.enabled) {
    return false;
}

History.Adapter.bind(window, 'statechange', function() {
    var State = History.getState();
    History.log(State.data, State.title, State.url);

    if (State.title == 'Page Header') {
        $('#content').load('/user/login/ .pageHeader > *');
    }

    if (State.title == 'Login Form') {
        $('#content').load('/user/login/ #common-form > *');
    }

    if (State.title == 'Nothing') {
        $('#content').empty()
    }
});

$('body').on('click', 'a', function(e) {

    var urlPath = $(this).attr('href');
    var Title = $(this).text();
    History.pushState(null, Title, urlPath);

    // prevents default click action of <a ...>
    return false;
});
<\script>

I would like to know if this is the correct usage. The previous version had an ability to bind to an event using the # urls. I have not seen any examples for binding events to urls with this latest version so I used the .on() click event to call History and sorted out what link was clicked in there.

I am not sure if this is the best way to accomplish this for this example.

  • 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-14T07:53:49+00:00Added an answer on June 14, 2026 at 7:53 am

    After working on this some more, I have come up with a simple, but complete example for how to use the latest History.js. Here is working jsfiddle example that does Ajax loads of HTML fragments hosted on Github

    <!DOCTYPE html>
    <html>
      <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <title>Simple History.js Ajax example by dansalmo</title>
    
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
        <script type="text/javascript" src="http://balupton.github.com/history.js/scripts/bundled/html4+html5/jquery.history.js"></script>
    
        <style type='text/css'>
          .hidden {
            display: none;
            visibility: hidden;
          }
        </style>
      </head>
      <body>
        <a href="/home">Home</a>
        <a href="/about">About</a>
        <a href="/contact">Contact</a>
        <a href="/other">Other</a>
    
        <p>The whole page will not re-load when the content below is updated, yet the URL is clean and the back button works!<p><br />
        <div id="content">
          <div id="home">Home Page content</div>
        </div>
        <br />
        <p>The content div will be updated with a selected div fragment from an HTML file hosted on github, however the broswer will see each content update request as a part of the page history so that the back button can be used.</p>
        <br  />
        <p>Adding more menu items is as simple as adding the new links and their corresponding html fragments.<p>
        <div id="hidden_content" class="hidden"></div>
      </body>
        <script type='text/javascript'>//<![CDATA[ 
      $(function(){
      var History = window.History;
      if (History.enabled) {
          State = History.getState();
          // set initial state to first page that was loaded
          History.pushState({urlPath: window.location.pathname}, $("title").text(), State.urlPath);
      } else {
          return false;
      }
    
      var loadAjaxContent = function(target, urlBase, selector) {
          $(target).load(urlBase + ' ' + selector);
      };
    
      var updateContent = function(State) {
          var selector = '#' + State.data.urlPath.substring(1);
        if ($(selector).length) { //content is already in #hidden_content
            $('#content').children().appendTo('#hidden_content');
            $(selector).appendTo('#content');
        } else { 
            $('#content').children().clone().appendTo('#hidden_content');
            loadAjaxContent('#content', State.url, selector);
        }
      };
    
      // Content update and back/forward button handler
      History.Adapter.bind(window, 'statechange', function() {
          updateContent(History.getState());
      });
    
      // navigation link handler
      $('body').on('click', 'a', function(e) {
          var urlPath = $(this).attr('href');
          var title = $(this).text();
          History.pushState({urlPath: urlPath}, title, urlPath);
          return false; // prevents default click action of <a ...>
      });
      });//]]>  
    
      </script>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was able to put together a encryption sample like below but during decryption
I'm new to Jquery and I was able to put together a simple toggle
I've put together some key frame animations in CSS which animate a div from
I hope I am able to put this question together well. In a partial
I've read about nullif , but haven't been able to put together a SELECT
I wonder whether someone may be able to help me please. I've put together
I wonder whether someone may be able to help me please. I've put together
I wonder whether someone may be able yo help me please. I've put together
I wonder whether someone may be able to help me please. I've put together
I wonder whether someone may be able to help me please. I've put together

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.