OnHashChange works onLoad ... inconvenient

Therefore, I want my page to load content if the window hash has changed.

Using Mootools, this is pretty simple:

$extend(Element.NativeEvents, {
    hashchange: 1
});

and then:

window.addEvent('hashchange', function() {});

However, the event is hashchangefired when the page loads, even if the specification requires that it not fire until the page has finished loading.

If I load the page for the first time, without a hash, then everything works as expected.

I think the problem is that the browser considers the page loading to be “completed” and then runs the rest of JavaScript, which includes determining the hash to load the desired page.

, http://foo.bar/, . , http://foo.bar/#test, , , .

, , "domready", THEN "" , onHashChange. ?

, , onHashChange. :

var noHashChange;
noHashChange = true;
var hashes = window.location.hash.substr(1).split("/"); // Deciphers the hash, in this case, hashes[0] is "test"
selectContent(hashes[0]); // Here, selectContent would read noHashChange, and wouldn't update the hash
noHashChange = false;

, . , 3 4 , . .

, - , , , :

window.location.hash = foobar;

... , , .

, ? , ( )...

, ...

+3
3

, , :

 if(window.location.hash != foobar){ window.location.hash = foobar;}

onHashChange ? , , , , .

( , , - . , , - ?)

0

-, , has changed.it . Object.prototype.watch
. : On - window.location.hash - ?

0

MooTools, onhashchange, himl5 history api , : )

0

Source: https://habr.com/ru/post/1754116/


All Articles