admin管理员组文章数量:1331613
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 going to django
folder will generate ajax request to ;_=1327709883334
which will return the html content of the folder. The link will also change to . 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.
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./django/django going to django
folder will generate ajax request to https://github./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./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.
Share Improve this question edited Jan 28, 2012 at 0:39 shadyabhi 17.2k28 gold badges85 silver badges134 bronze badges asked Jan 28, 2012 at 0:29 miki725miki725 27.9k17 gold badges108 silver badges122 bronze badges 2- It's not 100% clear if you're interested more in the URL change or in the overall UI experience - the URL change together with the smooth AJAX transition. – Dav Commented Jan 30, 2012 at 21:45
- my main interest is how they changed the url without reloading the whole thing. – miki725 Commented Jan 31, 2012 at 3:39
2 Answers
Reset to default 7You 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.
Well, as was described in the ments 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./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.
This will display the a
element responsible for the directory link.
A "suspect" class showed up, let's search for it on the javascript source of the page.
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.
本文标签: javascriptHow AJAX is done in github source browseStack Overflow
版权声明:本文标题:javascript - How AJAX is done in github source browse? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742223766a2435635.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论