附加包括hashtag到URL的JavaScript [英] Attaching hashtag to URL with javascript
问题描述
我希望在不牺牲搜索引擎建立一个Ajax网站。我的问题是: 如果我有我的网页链接是这样的:
I want to build an ajax site without sacrificing SEO. My question is: If I have link on my page like this:
<a href="http://mysite.com/cats" id="cats">Cats</a>
<a href="http://mysite.com/dogs" id="dogs">Dogs</a>
...每个链接被点击时,我想更新与相应的hashtag地址栏。所以,如果猫链接被点击的当前位置将是 http://mysite.com/#cats ,我可以用这个显示我的Ajax内容。如果JavaScript被关闭或用户的搜索引擎,他们将直接进入/猫
...when each link is clicked I would like to update the address bar with the corresponding hashtag. So, if "Cats" link is clicked the current location will be http://mysite.com/#cats and I can use this to show my ajax content. If javascript is off or user is search engine, they will go directly to /cats
推荐答案
您可以修改的location.hash
属性,它会改变当前锚标识符没有导航走形页面,例如,你可以:
You can change the location.hash
property, it will change the current anchor identifier without navigating away form the page, for example you could:
<a href="http://mysite.com/cats" id="cats" class="ajaxLink">Cats</a>
<a href="http://mysite.com/dogs" id="dogs" class="ajaxLink">Dogs</a>
然后:
$('.ajaxLink').click(function (e) {
location.hash = this.id; // get the clicked link id
e.preventDefault(); // cancel navigation
// get content with Ajax...
});
这篇关于附加包括hashtag到URL的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!