使用javascript将hashtag附加到URL

alo*_*cha 31 javascript ajax

我想在不牺牲SEO的情况下建立一个ajax网站.我的问题是:如果我的页面上有这样的链接:

   <a href="http://example.com/cats" id="cats">Cats</a>
   <a href="http://example.com/dogs" id="dogs">Dogs</a>
Run Code Online (Sandbox Code Playgroud)

...当点击每个链接时,我想用相应的标签更新地址栏.因此,如果单击"Cats"链接,则当前位置将为http://example.com/#cats,我可以使用它来显示我的ajax内容.如果javascript关闭或用户是搜索引擎,他们将直接转到/ cats

CMS*_*CMS 44

您可以更改location.hash属性,它将更改当前锚标识符而无需从页面导航,例如您可以:

<a href="http://mysite.com/cats" id="cats" class="ajaxLink">Cats</a>
<a href="http://mysite.com/dogs" id="dogs" class="ajaxLink">Dogs</a>
Run Code Online (Sandbox Code Playgroud)

然后:

$('.ajaxLink').click(function (e) {
  location.hash = this.id; // get the clicked link id
  e.preventDefault(); // cancel navigation

  // get content with Ajax...
});?
Run Code Online (Sandbox Code Playgroud)


小智 16

如果表单中有感叹号,Google会为哈希编制索引: #!dogs

然后它假设这些是面向AJAX的: