哈希导航URL构造

时间:2011-12-28 15:22:25

标签: javascript jquery redirect

我们现在说location.hrefhttp:/domain.com/en/

点击后我希望它为http://domain.com/en/#opened-File.html/1

这样我知道我需要什么URL,所以如果用户复制并共享我正在做的URL:

$(document).ready(function(){
    var info = window.location.hash.match(/^#([^\/]*)\/([^-]*)-(.*)$/),
        url="", nivel="", seccion="";
    if (info) {
        url = info[1];
        nivel = info[3];
        seccion = info[2];
        location.href = url;
    }
}

哪个好,但我的问题是:

  • 这是一个很好的方法吗?
  • 这是一个很好的吗?
  • 你会以不同的方式做吗?

这与

一起使用
$('nav a').each(function(){
        if(!$(this).hasClass('enlaceAnulado')){
        /*Recopilamos*/
            var href = $(this).attr('href');
            var id = $(this).attr('id');
            var parts = id.split("_");
            var seccion = parts[0];
            var nivel = parseInt(parts[1])+1;
        /*Quitamos el enlace*/
            $(this).attr('href','javascript:void(0)');
        /*Guardamos la información.*/
            $(this).data('hrefnot',href);
            $(this).data('nivel',nivel);
            $(this).data('seccion',seccion);
            $(this).addClass('enlaceAnulado');
        }
    });

所以这些链接是静态的,但我这样做是为了改善用户体验并通过ajax加载内容

3 个答案:

答案 0 :(得分:1)

搜索引擎会对您的网页内容编制索引,就好像该网址没有任何内容符合哈希值。散列导航仅用于浏览器维护导航历史记录。您应始终将要编入索引的内容静态化。将此视为对您的所有三个问题的回答。

答案 1 :(得分:0)

  

这是一个好方法吗?

我的第一个倾向是认为这对服务器端来说是一个很好的工作(php,python,asp.net,apache重写等)。

  

这是个热情的吗?

我会担心哈希值,而是使用更好的Url做法。

  你会以不同的方式做到吗?

我宁愿让我的服务器解析(mod重写等)Url而不是javascript。

答案 2 :(得分:0)

我想在Nikita Volkov的回答中添加以下内容:

搜索抓取工具通常不会运行JavaScript代码(尽管Google是trying to change that)。这意味着使用JavaScript将用户重定向到静态页面,就像您正在使用的那样:

location.href = url;

......不会起作用。

如果你想使用散列标签制作更具SEO性能的URL,你必须在服务器端进行。

相关问题