Adding xmlns tag to HTML Tag in IE using javascript. FB Like button/Send button doesn't render
I am trying to add fb like button and fb send button to a webpage.
I can't modify the html tag in the source to add the fbname space so I instead use client side JS to add the same.
var htmlRoot = jQuery(jQuery("html").get(0));
if(typeof(htmlRoot.attr("xmlns:fb")) == "undefined") {
htmlRoot.attr("xmlns:fb",'http://www.facebook.com/2008/fbml');
htmlRoot.attr("xmlns:og",'http://opengraphprotocol.org/schema/');
开发者_如何转开发 if (typeof(console) != 'undefined' && console) {
console.log("FB NameSpace added");
}
}
The FB.XFBML.parse function is called only after the namespace is added. If I do a get namespaces just before the parse function is called I can see that the html namespace is modified. This works well in all the browsers except IE. IE doesn't respect the changed name space. Even though logging the name space value shows that it has included the FBName space still the like buttons don't render. Any work around ?
Add conditional code which for IE calls document.namespaces.add()
if(document.namespaces) {
//IE
document.namespaces.add("fb", "http://ogp.me/ns#");
document.namespaces.add("og", "http://ogp.me/ns/fb#");
if (typeof(console) != 'undefined' && console) {
console.log("IE: OG and FB NameSpace added");
}
} else {
//Other Browsers
var htmlRoot = jQuery(jQuery("html").get(0));
if(typeof(htmlRoot.attr("xmlns:fb")) == "undefined") {
htmlRoot.attr("xmlns:og",'http://ogp.me/ns#');
htmlRoot.attr("xmlns:fb",'http://ogp.me/ns/fb#');
if (typeof(console) != 'undefined' && console) {
console.log("OG and FB NameSpace added");
}
}
}
do NOT put this into a $(document).ready() function!
精彩评论