Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How AJAX is done in github source browse?

Github has a really nice source browser. Navigating between different paths in the repo generates ajax calls to load the content (as you can clearly see in the firebug log). The ajax call returns the html of the new list of files to be displayed. In addition to changing the view list of files, the url changes as well. However it does not use fragments like most ajax deep-linked sites (use of #). At github the whole url changes.

For example at django repo at https://github.com/django/django going to django folder will generate ajax request to https://github.com/django/django/tree/master/django?slide=1&_=1327709883334 which will return the html content of the folder. The link will also change to https://github.com/django/django/tree/master/django. As you can see this new link does not use a fragment.

How is that done? I always thought that ajax based sites have to use url fragments (#) to achieve deep linking but apparently it is not so.

like image 765
miki725 Avatar asked Jan 28 '12 00:01

miki725


2 Answers

You have to use HTML5's pushState() method to change browser history.

window.history.pushState(data, "Title", "/new-url");

Doc says:

pushState() takes three parameters: a state object, a title (which is currently ignored), and (optionally) a URL.

The last argument is the new URL. For security reasons you can only change the path of the URL, not the domain itself. The second argument is a description of the new state. And the first argument is some data that you might want to store along with the state.

like image 83
Abhijeet Rastogi Avatar answered Sep 22 '22 23:09

Abhijeet Rastogi


Well, as was described in the comments by Dav, it appears that GitHub does not use the pAjax library. Due to the fact that I ended up answering with an "incorrect" information (actually I think I had seen something related to GitHub using pAjax when I was answering this question, but at the moment I can not find the source), I went after the correct answer.

I did not find anything official on the part of developers regarding whether any library was used, only found a post saying that the History API was used: https://github.com/blog/760-the-tree-slider

Then came to my head, why not ask the code itself?

Using Chrome (in reality any browser with a decent developer tools), open a repository (in this case, pAjax), right-clicking on any directory, simply choose inspect element.

Inpect Element

This will display the a element responsible for the directory link.

HTML Structure

A "suspect" class showed up, let's search for it on the javascript source of the page.

The Codez!

And here it's, the click event handler for the directory link, in addition to the entire code related to the animation and the History Api. And as can be noted, it's not used any library behind the History Api. Don't forget to mark the Pretty Print option.


Old and incorrect answer

GitHub uses the jQuery plugin pJax (pushState + Ajax), which uses the HTML5 history API.

like image 35
jonathancardoso Avatar answered Sep 20 '22 23:09

jonathancardoso