How can I change Firefox window.location.hash without creating a page reload?

I store some status information in a URL fragment (hash, as you would call it). When I change window.location.hashin Chrome and Safari, the page does not reload - this is the behavior I want. When I change window.location.hashin Firefox, I get a page reload. How to prevent this?

Note. My reason for storing state in the url is because userA can send the url to userB, and userB will be able to view the same page (loading AJAX).


Resolution: what is it for, in Firefox (only?) The page will reload if you completely delete the hash, including the "#" character. I ended up not deleting the entire hash.

+3
source share
5 answers

There is a jQuery plugin called historythat does this. I suggest you look at the source code (it's not very big the last time I checked). It also works for older versions of IE, but it's really nasty: P

+1
source

Assuming that this is Firefox’s behavior (reloading the page even if only the hash part changes) and cannot be overridden, perhaps you could write your own solution that finds the (x, y) position of the binding target and calls window.scroll(x,y).

+1
source

In the post you mentioned, here is the code that installs hash:

function (hsh) {
    hsh="#"+hsh;
    this.current.hash=hsh;
    document.location.hash=hsh;
}

And here is the code that reads hash(for example, from another URL):

function () {
    if(document.location.hash.split("#").pop()==this.current.hash.split("#").pop()) { return; }//this hash was set by code!
    var bl=-1;//-1 = none; otherwise, it a block index.
    var cp=-1;
    if(document.location.hash.indexOf(Un.HASH_BLOCK_PREFIX)>-1) {
        var blkhsh=document.location.hash.substring(Un.HASH_BLOCK_PREFIX.length+1);
        var blknum=parseInt(blkhsh,16);
        if(!isNaN(blknum)) {
            for(bi in Un.BLOCKS.blFrom) {
                if(Un.BLOCKS.blFrom[bi]==blkhsh) {
                    bl=bi; break;
                }
            }
        }
        else {
            var blkspc=blkhsh.split(Un.HASH_BLOCK_SPACE).join(" ");
            for(bi in Un.BLOCKS.blName) {
                if(Un.BLOCKS.blName[bi]==blkspc) {
                    bl=bi; break;
                }
            }
        }
    }
    else if(document.location.hash!="") {
        var hexhsh=document.location.hash.split("#").pop().split(Un.HASH_CP_PREFIX).pop().split("x").pop(); //if the cp_prefix is missing, or if prefixed with x, this will still work :)
        if(hexhsh.length==4 && !isNaN(parseInt(hexhsh,16))) {
            cp=hexhsh;
            for(bi in Un.BLOCKS.blFrom) {
                if(Un.BLOCKS.blFrom[bi]>cp) {
                    bl=bi-1;
                    break;
                }
            }
        }
    }
    if(bl>-1) {
        Un.selectBlock(bl,(cp==-1 ? 0 : 1));
    }
    else {
        Un.next(1);
        Un.setMenuButton($("#BlockButton"),0);
    }
    if(cp!=-1) {
        setTimeout("Un.cpOpen('"+cp+"')",100);
    }
}
+1
source

All Articles