开发者

how to dynamically alter the hash location un a url based on link clicks

I have this working with just one instance of links, but I wanted to consolidate my code and re-use the same snippet for each instance of links.

Presently I have working:

$("nav a").live('click', function(){
        window.location.hash = '!/' + usr + '/' + $(this).attr("href").replace('.php', '/');
        origHash = $(this).attr("href");
        return false;
    });
$("#files-left-pane a").live('click', function(){
        window.location.hash = '!/' + usr + '/files/' + $(this).attr("href").replace('.php', '/');
        origHash = $(this).attr("href");
        return false;
    });
    $(window).bind('hashchange', function(){
            dump = window.location.hash;
            newHash = window.location.hash.substring(3).replace(u开发者_StackOverflowsr + '/', '').replace('/', '.php');//.replace('files/', '');
            //newHash = window.location.hash.replace(dump, origHash, 'g');
            console.log(newHash);
            if (newHash) {
                $wrap.find("#main-content").fadeOut(200, function() {
                    $wrap.load(newHash + " #main-content", function() {
                        $content.fadeIn(function() {
                            $wrap.animate({
                                height: baseHeight + $content.height() + "px"
                            }, 500);
                        });
                    });
                });
            }
    });

right now, if a user clicks on $("nav a") it will make window.location.hash look like this

(in this example the user clicks on <a href="files.php">Files</a>) www.site.com/#!/s2xi/files/

the $(window).bind('hashchange') will then translate the hash into

www.site.com/files.php

Then, if a user clicks on $("#files-left-pane a") which is in a sub menu located in files.php. The window.location.hash will look like this :

(in this example the user clicks on <a href="buy.php">Buy</a>) www.site.com/#!/s2xi/files/buy/

the $(window).bind('hashchange') should then translate the hash into

www.site.com/buy.php


If all you want is take the last word and add ".php", thats easy You have two ways for that, i think the easiest one is split (the other one is regex).

var splittedHash = window.location.hash.split("/")
newHash = splittedHash[splittedHash.length - 2] + ".php";

Should easily do the trick.

By the way, the regex version is:

newHash =  window.location.hash.match(/[^\/]*\/$/)[0].replace("/","") + ".php"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜