开发者

How to avoid re-parsing of XFBML elements?

开发者 https://www.devze.com 2023-02-07 18:31 出处:网络
I am using the JavaScript Facebook SDK to parse the xfbml elements. However, I also use ajax pagination. Here is what happens.

I am using the JavaScript Facebook SDK to parse the xfbml elements. However, I also use ajax pagination. Here is what happens.

I load the first page of elements, which each contain a like button and a comment box in XFBML. I call the parse method of the SDK to parse the elements.

If the user loads the next page of the elements, they get added to the DOM via Ajax. However, when I now call the parse methode, the elements of the first page get parsed again, which is obviously not desired.

I know that the parse method offers a nother entry root, but since all the objects are siblings, this is not an option for me. is there any way to tell the parse to only parse elements he has not parsed yet? or will i have to remove all开发者_开发知识库 the old xfbml tags that already have been parsed?


FB.XFBML.parse() is the only available function for re-parsing XFBML, and you can either pass it a DOM element as a starting point, or pass nothing and have it parse your entire DOM structure.

Since those are your only options, I would suggest that you modify your Ajax call. Instead of having the Ajax call directly insert your new elements into their final DOM location, making them harder to re-parse, why not create a <div> (or some other element), load your Ajax-fetched content into it, and then call FB.XFBML.parse() on the <div> before moving the contents into the appropriate place? You could hide the <div>, and use the second callback parameter of the parse function to do the move:

//do Ajaxy stuff here to insert new content into hidden div 'foo'

FB.XFBML.parse(document.getElementById('foo'), function() {        
    document.getElementById('yourContent').innerHTML += document.getElementById('foo').innerHTML;
});

I haven't done Javascript in awhile, so pardon any egregious syntax, but hopefully you get the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消