如何在我们的URL中制作/#!/?

mar*_*agz 4 javascript jquery

大家!

这个问题我差不多3个小时!如果twitter.com/#!/username然后页面/个人资料是用户名,请在Twitter中复制这个概念

http://twitter.com/#!/mardagz因为你可以看到它被重定向到我的帐户,所以现在我试图通过获取当前的URL来制作我自己的...然后我修剪为字符串并将其拆分(/#! /)当我尝试申请什么工作时......

假设当前网址是http://www.domain.com/#!/about

代码:

$(window).load(function () {
      var getUrl = top.location;
      var trimUrl = jQuery.trim(getUrl);
      var splitUrl = trimUrl.split('/#!/')
      //alert(splitUrl[1]);
      switch(splitUrl[1])
        {
            case 'home':
            //Do Something
            break;
            case 'skill':
            //Do Something
            break;
            case 'about':
            //Do Something go to About Us Page!
            break;
        }
    });
Run Code Online (Sandbox Code Playgroud)

owhh它不工作......有谁有解决方案吗?:) 先感谢您.. :)

Rob*_*b W 6

如果要检测哈希更改,请使用该hashchange事件.如果您的顶级窗口位于同一个域,则下面的代码将像魅力一样工作.否则,同源策略将阻止您的脚本访问location顶层文档的对象.

function hashChanged() {
  var getUrl = top.location.hash.substr(3); // #!/ = 3 characters
  switch(getUrl)
    {
        case 'home':
        //Do Something
        break;
        case 'skill':
        //Do Something
        break;
        case 'about':
        //Do Something go to About Us Page!
        break;
    }
}
$(window).load(function(){
    // Set handler
    $(window).bind('hashchange', hashChanged);

    // Handle hash on load
    hashChanged();
});
Run Code Online (Sandbox Code Playgroud)