搜索引擎的蜘蛛只識(shí)別href的一般超鏈接,而不識(shí)別JavaScript代碼,遇到一般超鏈接就會(huì)爬進(jìn)去,遇到JavaScript不會(huì)爬進(jìn)去。即,搜索引擎抓不到AJAX動(dòng)態(tài)加載的內(nèi)容。
AJAX異步加載文章一般步驟
一、新建一個(gè)AJAX.ashx的一般處理程序
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace AJAX { /// <summary> /// AJAX 的摘要說明 /// </summary> public class AJAX : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; int id=int.Parse(context.Request["id"]); if (id==1){ context.Response.Write("藍(lán)牙技術(shù)..."); }else if (id==2){ context.Response.Write("巴金 《家》..."); }else if (id==3){ context.Response.Write("百度百科詞條..."); } } public bool IsReusable { get{return false;} } } }
二、新建一個(gè)Page.html靜態(tài)頁(yè)面
<!DOCTYPE html> <html> <head> <title></title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> function LoadArt(id) { $.post("AJAX.ashx", { "id": id }, function (data) { $("#p1").text(data); }); } </script> </head> <body> <p id="p1"></p> <a href="javascript:void(0)" onclick="LoadArt(1)">1</a> <a href="javascript:void(0)" onclick="LoadArt(2)">2</a> <a href="javascript:void(0)" onclick="LoadArt(3)">3</a> </body> </html>
上面的代碼不利于SEO優(yōu)化,蜘蛛不會(huì)爬進(jìn)去收錄AJAX頁(yè)面,也就是讀不到任何內(nèi)容,因?yàn),href是JS代碼。
<a href="javascript:void(0)" onclick="LoadArt(1)">1</a>
利于SEO的AJAX異步加載
<!DOCTYPE html> <html> <head> <title></title> <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> function LoadArt(id) { $.post("AJAX.ashx", { "id": id }, function (data) { $("#p1").text(data); }); } </script> </head> <body> <p id="p1"></p> <a href="AJAX.ashx?id=1" onclick="LoadArt(1);return false;">1</a> <a href="AJAX.ashx?id=1" onclick="LoadArt(2);return false;">2</a> <a href="AJAX.ashx?id=1" onclick="LoadArt(3);return false;">3</a> </body> </html>
href是個(gè)一般的超鏈接,蜘蛛就會(huì)爬進(jìn)去,有利于SEO。但點(diǎn)擊標(biāo)題,會(huì)彈出一個(gè)新頁(yè)面,添加:return false; 即可。
很多網(wǎng)站都用的是這個(gè)技術(shù),即實(shí)現(xiàn)了使用JS和AJAX,也可以讓搜索引擎抓到動(dòng)態(tài)加載的內(nèi)容。