在创建链接时,我需要了解是否可以执行以下操作:
<a href="page.html" onclick="javascript:ajax();">blah</a>
我希望用户单击它的时候,它将获得我们使用Ajax描述的内容,但是,我希望搜索引擎能够跟踪链接,这样我们仍然可以获得最大的索引。我确定可以这样操作,但不知道具体该怎么做。
Set the href
attribute of the link to the static page that you want the search engine to follow, then use the onclick
event to do your javascript/ajax request for "human" users. As long as the onclick event returns false
, the standard link won't be followed.
A good test of this would be turning javascript off and clicking the link - you should end up with what you want the search engine to see.
You don't need the "javascript:
" string in the onclick attribute, it is only necessary if you are putting javascript in the href
attribute. You should have something like:
<a href="page.html" onclick="ajax(this.href); return false;">blah</a>
I asked similar question, where I got this answer from pekka:
The best way would be to degrade gracefully, e.g. by using a standard
<a id='mylink' href='xyz.html'>
link that points to the resource that is opened in the popup. You would then add the JQuery code to the link, causing it to open in the pop-up.
That way, even users that do not have JavaScript turned on can access your popup.
Most Lightbox clones like Thickbox work that way.