如何在Facebook上分享ajax内容



原来我的网站有不同的部分由ajax加载。例如,我想在facebook上分享联系人部分,但没有办法做到这一点,因为他们总是共享索引。

我的网页的url当ajax加载内容是好的

www.example.com/contacto

加载ajax内容的按钮:

<a href="#contacto" onClick="cargarbio('ficha.php','contenido-texto')">CONTACTO</a>
Ajax代码:

 function nuevoAjax(){
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
// Función para cargar los contenidos de forma asíncrona.
// + pagina: fichero cuyo contenido queremos cargar.
// + identidicador del elemento en el que se cargará el nuevo contenido.
function cargarbio(pagina,destino){
var contenedor;
var ajax;
contenedor = document.getElementById(destino);
ajax = nuevoAjax();
ajax.open("GET", pagina, true);
ajax.onreadystatechange=function() {
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText;

//do Ajaxy stuff here to insert new content into hidden div 'foo' FUN
FB.XFBML.parse(document.getElementById('contenido-texto'), function() {        
    document.getElementById('yourContent').innerHTML += document.getElementById('contenido-texto').innerHTML;
});
//FUN

}
}
ajax.send(null);
}

对于动态内容,你必须使用预渲染服务,如http://prerender.io,因为Facebook Scraper不解释JS代码。因此,行为是你所描述的…

  • https://developers.facebook.com/docs/sharing/best-practices/
  • https://developers.facebook.com/docs/sharing/webmasters/crawler

最新更新