How can I change the page URL without refreshing the page?
-
How do I change the URL without refreshing the page? I've built a website with CodeIginer and I want to implement AJAX and JQuery in one of my pages. The problem is; when I load the content, the URL doesn't change. Let's say that I have the URL http://www.example.com/controller/function/param And another URL : http://www.example.com/controller/function/param2 How can I change the first URL to the second one when I click a button?
-
Answer:
In HTML5 you can change the URL: window.history.pushState("object or string", "Title", "/new-url"); check http://spoiledmilk.dk/blog/html5-changing-the-browser-url-without-refreshing-page docs: https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history#The_pushState().c2.a0method UPDATE An overview of which browser support the new HTML5 history API: http://caniuse.com/#search=pushState (caniuse.com is worth to bookmark!) there are already frameworks that do the hard work for you and even gracefully fallback to the common hash-tag solution: https://github.com/balupton/History.js/ History.js gracefully supports the HTML5 History/State APIs (pushState, replaceState, onPopState) in all browsers. Including continued support for data, titles, replaceState. Supports jQuery, MooTools and Prototype. For HTML5 browsers this means that you can modify the URL directly, without needing to use hashes anymore. For HTML4 browsers it will revert back to using the old onhashchange functionality. http://documentcloud.github.com/backbone/ Backbone supplies structure to JavaScript-heavy applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing application over a RESTful JSON interface. ... pushState support exists on a purely opt-in basis in Backbone. Older browsers that don't support pushState will continue to use hash-based URL fragments, and if a hash URL is visited by a pushState-capable browser, it will be transparently upgraded to the true URL. http://mootools.net/forge/p/history MooTools is a compact, modular, Object-Oriented JavaScript framework designed for the intermediate to advanced JavaScript developer. [...] History Management via popstate or hashchange. Replaces the URL of the page without a reload and falls back to Hashchange on older browsers. http://dojotoolkit.org/reference-guide/dojox/app.html Dojo saves you time and scales with your development process, using web standards as its platform. It’s the toolkit experienced developers turn to for building high quality desktop and mobile web applications. [...] dojox.app manage the navigation history through HTML5 pushState standard and delegate it to browser enabled history management. ... just to name a few. (!!) BE AWARE One important side-effect when using the pushState (citation from the http://documentcloud.github.com/backbone/#History): Note that using real URLs requires your web server to be able to correctly render those pages, so back-end changes are required as well. For example, if you have a route of /documents/100, your web server must be able to serve that page, if the browser visits that URL directly. For full search-engine crawlability, it's best to have the server generate the complete HTML for the page ... but if it's a web application, just rendering the same content you would have for the root URL, and filling in the rest with Backbone Views and JavaScript works fine.
Bobby Stenly at Stack Overflow Visit the source
Other answers
Could use a hash (#) and put whatever you like afterwards. Here's a site I built using this - and then I have JavaScript read the hash and call appropriate functions: http://bannerhouse.com.au/#/popup=media&id=don Side note: This is useful for flash websites or flash content as well; you can use FlashVars to parse the hash value to the SWF and load an appropriate section/screen based on that.
Marty Wallace
Use hash tags via Javascript, so in the button's click event handler: location.hash = "param2" Which will change http://example.com/mypage/#whatever to http://example.com/mypage/#param2 Of course, you could also put your "folders" after the hash, so, with a base url of http://example.com/ you then add: location.hash = "/MyPage/MySubPage/MyInfo"; which changes it to http://example.com/#/MyPage/MySubPage/MyInfo
Thomas Shields
A http://stackoverflow.com/questions/1488165/javascript-changing-the-get-parameter-without-redirecting deduced no, unless using a hash tag. One idea put forward, but strongly discouraged, was using a 204 HTTP response. From the W3: No Response 204 Server has received the request but there is no information to send back, and the client should stay in the same document view. This is mainly to allow input for scripts without changing the document at the same time.
uncollected
Related Q & A:
- How can I change url name?Best solution by Yahoo! Answers
- How can I change back to the old Yahoo! home page?Best solution by Yahoo! Answers
- How do I change my page back to the new page after I changed it to the classic can?Best solution by Yahoo! Answers
- How can I erase all my mail without having to do it page by page?Best solution by Yahoo! Answers
- How can i change my email address, without losing my old emails?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.