I use javascript function below to move embed Facebook Meta data to head, This works fine for visitors but not when FB crawls the page, How can i execute this when Facebook crawling the page ?
function metaBodyToHead() {
var head = document.head,
metaTAGs = document.getElementsByTagName( "DIV" );
for( var i = 0, ln = metaTAGs.length; i < ln; i++ ) {
head.appendChild( metaTAGs[ i ].parentNode.removeChild( metaTAGs[ i ] ) );
}
}
metaBodyToHead();
I need to get the meta data in head when facebook crawls the page
Test page: http://greenboxmedia.org/meta.html
Facebook debug: https://developers.facebook.com/tools/debug/og/object?q=www.greenboxmedia.org%2Fmeta.html
How can i execute this when Facebook crawling the page ?
Not at all, of course.
Facebook’s scraper just looks at the HTML code of your page; it’s not a full-fledged “browser” that would execute any client site code.
You’ll have to put your OG meta data into the HTML code that get’s delivered when the URL is requested. That’s how it works.
来源:https://stackoverflow.com/questions/11592102/execute-javascript-when-facebook-crawls-website