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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:52:30+00:00 2026-05-26T22:52:30+00:00

I see that Lifehacker is able to change the url while using AJAX to

  • 0

I see that Lifehacker is able to change the url while using AJAX to update part of the page. I guess that can be implemented using HTML5 or history.js plugin, but I guess lifehacker is using neither.

Does any one has a clue on how they do it?
I am new to AJAX and just managed to update part of the page using Ajax.


Thank you @Robin Anderson for a detailed step by step algo. I tried it and it is working fine. However, before I can test it on production, I would like to run by you the code that I have. Did I do everything right?

<script type="text/javascript">  
var httpRequest;  
var globalurl;
    function makeRequest(url) {  
    globalurl = url;
    /* my custom script that retrieves original page without formatting (just data, no templates) */
    finalurl = '/content.php?fname=' + url ;

    if(window.XMLHttpRequest){httpRequest=new XMLHttpRequest}else if(window.ActiveXObject){try{httpRequest=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{httpRequest=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){}}}  

    /* if no html5 support, just load the page without ajax*/
    if (!(httpRequest && window.history && window.history.pushState)) {     
            document.href = url;
            return false;  
    } 

    httpRequest.onreadystatechange = alertContents;  
    alert(finalurl);    /* to make sure, content is being retrieved from ajax */
    httpRequest.open('GET', finalurl);  
    httpRequest.send();  
    } 

    /* for support to back button and forward button in browser */
    window.onpopstate = function(event) {
            if (event.state !== null) {
                    document.getElementById("ajright").innerHTML = event.state.data;
            } else {
                    document.location.href = globalurl;
                    return false;
            };
    };    

    /* display content in div */
    function alertContents() {  
            if (httpRequest.readyState === 4) {  
            if (httpRequest.status === 200) {  
                    var stateObj = { data: httpRequest.responseText};
                    history.pushState(stateObj, "", globalurl);     
                    document.getElementById("ajright").innerHTML = httpRequest.responseText;
            } else {  
                    alert('There was a problem with the request.');  
            }  
            }  
    }  
    </script> 

PS: I do not know how to paste code in comment, so I added it here.

  • 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-05-26T22:52:30+00:00Added an answer on May 26, 2026 at 10:52 pm

    It is not an requirement to have the markup as HTML5 in order to use the history API in the browser even if it is an HTML5 feature.

    One really quick and simple implementation of making all page transistions load with AJAX is:

    1. Hook up all links except where rel=”external” exist to the function “ChangePage”
    2. When ChangePage is triggered, check if history API is supported in the browser.
    3. If history API isn’t supported, do either push a hashtag or make a normal full page load as fallback.
    4. If history API is supported:
      1. Prevent the normal link behaviour.
      2. Push the new URL to the browser history.
      3. Make a AJAX request to the new URL and fetch its content.
      4. Look for your content div (or similar element) in the response, take the HTML from that and replace the HTML of the corresponding element on the current page with the new one.

    This will be easy to implement, easy to manage caches and work well with Google’s robots, the downside is that is isn’t that “optimized” and it will be some overhead on the responses (compared to a more complex solution) when you change pages.

    Will also have backward compatibility, so old browsers or “non javascript visitors” will just get normal page loads.

    Interesting links on the subject

    • History API Compatibility in different browsers
    • Mozillas documentation of the History API

    Edit:

    Another thing worth mentioning is that you shouldn’t use this together with ASP .Net Web Forms applications, will probably screw up the postback handling.

    Code addition:

    I have put together a small demo of this functionality which you can find here.

    It simply uses HTML, Javascript (jQuery) and a tiny bit of CSS, I would probably recommend you to test it before using it. But I have checked it some in Chrome and it seems to work decent.

    Some testing I would recommend is:

    • Test in the good browsers, Chrome and Firefox.
    • Test it in a legacy browser such as IE7
    • Test it without Javascript enabled (just install Noscript or similar to Chrome/Firefox)

    Here is the javascript I used to achieve this, you can find the full source in the demo above.

    /*
        The arguments are:          
            url: The url to pull new content from
            doPushState: If a new state should be pushed to the browser, true on links and false on normal state changes such as forward and back.
    */
    function changePage(url, doPushState, defaultEvent)
    {
        if (!history.pushState) { //Compatability check
            return true; //pushState isn't supported, fallback to normal page load
        }
    
        if (defaultEvent != null) { 
            defaultEvent.preventDefault(); //Someone passed in a default event, stop it from executing
        }
    
        if (doPushState) {  //If we are supposed to push the state or not
            var stateObj = { type: "custom" }; 
            history.pushState(stateObj, "Title", url); //Push the new state to the browser
        }               
    
        //Make a GET request to the url which was passed in
        $.get(url, function(response) {             
            var newContent = $(response).find(".content");      //Find the content section of the response
            var contentWrapper = $("#content-wrapper");         //Find the content-wrapper where we are supposed to change the content.
            var oldContent = contentWrapper.find(".content");   //Find the old content which we should replace.
    
            oldContent.fadeOut(300, function() { //Make a pretty fade out of the old content
                oldContent.remove(); //Remove it once it is done
                contentWrapper.append(newContent.hide()); //Add our new content, hidden
                newContent.fadeIn(300); //Fade it in!
            });
    
        });
    }
    
    
    //We hook up our events in here
    $(function() {
        $(".generated").html(new Date().getTime()); //This is just to present that it's actually working.
    
        //Bind all links to use our changePage function except rel="external"
        $("a[rel!='external']").live("click", function (e) { 
            changePage($(this).attr("href"), true, e);
        });
    
        //Bind "popstate", it is the browsers back and forward
        window.onpopstate = function (e) { 
            if (e.state != null) {
                changePage(document.location, false, null); 
            }
        }
    }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see that EF can update a model based on an existing database schema.
i see that in tableSorter you can sort one page at a time which
I see that SharePoint 2010 makes javascript ajax calls to some pretty slick internal
I see that if we change the HOME (linux) or USERPROFILE (windows) environmental variable
I see that SRFI 4 does not mention resizing of vectors. I'm using f64vectors
I see that I can retrieve CMAttitude from a device and from it I
I see that I can remove all of the UIPageViewController gestures, but what if
I see that can use ASP_regiis to encrypt sections of the web.config file, but
I see that there are two options that I know can be used in
I see that the SML/NJ includes a queue structure. I can't figure out how

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.