2

I found an app on the internet inside of which, when you click A link it will redirect to A page without refreshing the whole place and changing the url address bar, I know its possible becuase JQuery has access to the client sode browser.

Please help me with this.

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
Spudster
  • 69
  • 1
  • 11

5 Answers5

8

using the history api you could do something like this

  if (history && history.pushState){
     history.pushState(null, null, '/new/url');
  }

try it in your browser now with chrome js console

thats all it takes (maybe a little more, but thats the basis of it

read more in http://diveintohtml5.info/history.html

Nick Ginanto
  • 31,090
  • 47
  • 134
  • 244
3

You can use HTML5 History API to to change the URL without refreshing the page, check out this tutorial from Mozilla https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history
Also check out https://github.com/browserstate/History.js for cross-browser solution

The jQuery PJAX plugin https://github.com/defunkt/jquery-pjax will help you do what you have mentioned, it will help to update the content and change the URL without refresh.

Here is an example: http://pjax.heroku.com/
Source code for this example is available here: https://github.com/defunkt/jquery-pjax/tree/heroku

  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer (such as an example usage of the API) here, and provide the link for reference. – John Dvorak Jun 18 '13 at 10:36
2

This is commonly done by either using the latest HTML5 history API, or, for old browsers, changing the hash in the URL. Have a look at this article: http://dev.opera.com/articles/view/introducing-the-html5-history-api/.

Whenever some user action prompts a new page, instead of loading the new page (with a new URL), the page retains the common properties (like say, the header, footer, or say, in a eCommerce site, the shopping cart widget), and the remaining contents are fetched asynchronously through Ajax. Once the new content is fetched, the UI is changed accordingly, and a new state pushed into the History stack, and changing the URL accordingly. Through the data attribute used here as a dictionary, you store information about what to show when the user navigates forward/backward and arrives at that page again. Something like this:

history.pushState({mydata1: data, mydata2: anotherData},"newTitle",newURL);

So when the user presses the Back button of the browser, that entry is simply popped off the history stack, and from the data attributes of the new entry in the top of the stack, the new page is shown. The current page can be updated as well:

history.replaceState({mydata1: data, mydata2: anotherData}, "anotherTitle",newURL);

All information regarding the current page can be obtained from state:

myState = history.state;

If you have observed, Facebook does the same thing. When you click the Home button, or go a friend's profile, no new page is loaded. The notification ticker on the right remains as it is (instead of loading again for the new page), if you have any chat box opened, it stays as it is. This is because the page doesn't reload to the new page (in which case, all widgets, including chat and ticker should reload as well), only the URL is changed dynamically, and the new content is fetched and displayed through Ajax.

If you view Facebook in IE8, which doesn't support History API, you will find it still works, but the URL change behavior is different. It is because they fall back on the good old way of changing URLs by changing the hash.

SexyBeast
  • 7,913
  • 28
  • 108
  • 196
  • Ive also noticed facebook runs slower when java script is disabled just tested it. Where could I get more documentations on this method, sounds promising. – Spudster Jun 18 '13 at 12:22
  • I don't think you have noticed correctly enough. Most things in Facebook should completely stop working if you disable Javascript, automatic updates being one of them. What type pf documentation you are looking for exactly? The link I provided gives a nice working example.. – SexyBeast Jun 18 '13 at 13:04
  • Most things on facebook does stop working including the chat system which doesn't display just didn't mention them. I'm looking for anything that can changing the browser url real time or with minimal refesh like the linked I showed above. – Spudster Jun 19 '13 at 10:27
  • Please try out the article I mentioned. It includes a fairly easy to follow example. – SexyBeast Jun 19 '13 at 10:43
0

The simplest solution is to use a frame or iframe and set the target attribute of the a to the frame (or just click a link within a frame). This will show the new page within the frame and with the frameset-html's address in the location bar.

urzeit
  • 2,863
  • 20
  • 36
0

If you only need to print some info, doing it after de dash # symbol will not reload the document

Edorka
  • 1,781
  • 12
  • 24