更新:
後,我通過閱讀facebook dev reference。我爲你做了一個demo。您也可以在http://jsbin.com/onehul/17/edit編輯代碼。
基本上,你需要在HTML documentElement
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
這增加了XML命名空間確保瀏覽器會接受並解析從Facebook, 非標準標籤,然後dynamiclly創建腳本標籤附加到fb-root
DIV最後,做清理。腳本成功加載後,將其從Dom樹中移除。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div id="fb-root">
</div>
<script>
var script,
head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
script = document.createElement("script");
script.async = "async";
script.charset = "utf-8";
script.src = "http://connect.facebook.net/en_US/all.js#xfbml=1";
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
// Remove the script
if (head && script.parentNode) {
head.removeChild(script);
}
// Dereference the script
script = undefined;
}
};
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709 and #4378).
document.getElementById('fb-root').appendChild(script);
</script>
<fb:like-box href="http://facebook.com/pages/MyJobMag/165211540158300" width="336" show_faces="false" stream="false" header="true" colorscheme="light"></fb:like-box>
</body>
</html>
下面的代碼是借用和從jquery的源代碼修改以dynamiclly加載腳本,它都應該瀏覽器
var script,
head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
script = document.createElement("script");
script.async = "async";
script.charset = "YOUR SCRIPT CHARSET";
script.src = "YOUR SCRIPT SRC";
// Attach handlers for all browsers
script.onload = script.onreadystatechange = function() {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
// Remove the script
if (head && script.parentNode) {
head.removeChild(script);
}
// Dereference the script
script = undefined;
};
}
// Use insertBefore instead of appendChild to circumvent an IE6 bug.
// This arises when a base node is used (#2709 and #4378).
head.insertBefore(script, head.firstChild);
感謝,我修改它下作品;它仍然顯示在Chrome和Mozilla中,但仍不顯示在Internet Explorer中。 –
我還發現,如果Facebook腳本也在等待加載事件,並且直到事件觸發後才加載腳本,則腳本可能會混亂。 – jfriend00
你什麼時候打電話給任何FB功能?你是否在等待FB腳本加載? – jfriend00