简体   繁体   中英

How to change my Url without reloading the browser?

Basically, when I click a link on my homepage, I want to use some JavaScript to slid in a new div and change the url so that if the url was typed, it would bring the right info. I dont want the entire page to load when a link is click. While surfing from link to link, I want to animate the webs change instead loading a new page.

There are 3 ways of achieving this:


By doing this i mean literally do:

<a href="javascript:goToHome()">Home page</a>

You could do this, but this means you will be polluting the global scope with global variables and functions, which is strictly not recommended.

This is the most common and safest way to do it, since is available in almost all major browsers and their different versions 这是最常见,最安全的方法,因为几乎所有主流浏览器及其不同版本都提供该功能

<a href="#/home/">Home page</a>

The intention of the hash is to take you directly to a section within the document you are currently reading, that section is defined by the id attribute in HTML, this was intended for very large HTML documents. With the hash and the window.onhashchange event you can get what you want, except for some old versions of which i think are and below. (我认为是及以下)。

window.addEventListener('hashchange', function(event) {
    //your navigation logic here
});

More on hashchange : onhashchange event

This is the newest way to achieve it, since it's quite new not all browsers support it, but it's a nice way to do it: 这是实现它的最新方法,因为它很新,并不是所有的浏览器都支持它,但这是一个很好的实现方法:

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "bar.html");

Where:
stateObj : is an object that can be passed between history states.
title : Just a title for the new state.
url : The new URL to be displayed in the address bar.
This function call will trigger the window.onpopstate event on the browser.

More on History API: HTML5 History API

Hope this can help you.

You can do this:

window.location.hash = "id of the element to be scrolled to";

You can use this code, which I've taken from css-tricks

$(function () {
    $('a[href*=#]:not([href=#])').click(function () {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
            if (target.length) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
});

The above will automatically do the work for you.

Demo

If you want to work with "old" browsers then window.location.hash is your only option, and this can't manipulate the whole URL, only manipulate for example #about to the end.

If you're willing to forgo old browsers then you can go window.history.pushState as described here: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

Here is an overview for browser compatibility with pushState: http://caniuse.com/#search=pushstate

There's more information in this thread: How can I change the page URL without refreshing the page?

You might want to look at ember.js. It is a framework that does exactly what you're asking about (if I understood you correctly) and it handles all of the history for you. There's a helpful tutorial on code school to get you going that walks you through building a simple bootstrap site at: https://www.codeschool.com/courses/warming-up-with-emberjs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM