On - [removed].hash - Change?

后端 未结 13 1347
谎友^
谎友^ 2020-11-21 13:38

I am using Ajax and hash for navigation.

Is there a way to check if the window.location.hash changed like this?

http://example.com/blah

相关标签:
13条回答
  • 2020-11-21 13:43

    Ben Alman has a great jQuery plugin for dealing with this: http://benalman.com/projects/jquery-hashchange-plugin/

    If you're not using jQuery it may be an interesting reference to dissect.

    0 讨论(0)
  • 2020-11-21 13:50
    var page_url = 'http://www.yoursite.com/'; // full path leading up to hash;
    var current_url_w_hash = page_url + window.location.hash; // now you might have something like: http://www.yoursite.com/#123
    
    function TrackHash() {
        if (document.location != page_url + current_url_w_hash) {
            window.location = document.location;
        }
        return false;
    }
    var RunTabs = setInterval(TrackHash, 200);
    

    That's it... now, anytime you hit your back or forward buttons, the page will reload as per the new hash value.

    0 讨论(0)
  • 2020-11-21 13:52

    A decent implementation can be found at http://code.google.com/p/reallysimplehistory/. The only (but also) problem and bug it has is: in Internet Explorer modifying the location hash manually will reset the entire history stack (this is a browser issue and it cannot be solved).

    Note, Internet Explorer 8 does have support for the "hashchange" event, and since it is becoming part of HTML5 you may expect other browsers to catch up.

    0 讨论(0)
  • 2020-11-21 13:53

    There are a lot of tricks to deal with History and window.location.hash in IE browsers:

    • As original question said, if you go from page a.html#b to a.html#c, and then hit the back button, the browser doesn't know that page has changed. Let me say it with an example: window.location.href will be 'a.html#c', no matter if you are in a.html#b or a.html#c.

    • Actually, a.html#b and a.html#c are stored in history only if elements '<a name="#b">' and '<a name="#c">' exists previously in the page.

    • However, if you put an iframe inside a page, navigate from a.html#b to a.html#c in that iframe and then hit the back button, iframe.contentWindow.document.location.href changes as expected.

    • If you use 'document.domain=something' in your code, then you can't access to iframe.contentWindow.document.open()' (and many History Managers does that)

    I know this isn't a real response, but maybe IE-History notes are useful to somebody.

    0 讨论(0)
  • 2020-11-21 13:55

    HTML5 specifies a hashchange event. This event is now supported by all modern browsers. Support was added in the following browser versions:

    • Internet Explorer 8
    • Firefox 3.6
    • Chrome 5
    • Safari 5
    • Opera 10.6
    0 讨论(0)
  • 2020-11-21 13:56

    You could easily implement an observer (the "watch" method) on the "hash" property of "window.location" object.

    Firefox has its own implementation for watching changes of object, but if you use some other implementation (such as Watch for object properties changes in JavaScript) - for other browsers, that will do the trick.

    The code will look like this:

    window.location.watch(
        'hash',
        function(id,oldVal,newVal){
            console.log("the window's hash value has changed from "+oldval+" to "+newVal);
        }
    );
    

    Then you can test it:

    var myHashLink = "home";
    window.location = window.location + "#" + myHashLink;
    

    And of course that will trigger your observer function.

    0 讨论(0)
提交回复
热议问题