0
I need to create a Embedded Post on a page through a Click event. I am using the following function:
<body>
<div id="fb-root"></div>
<div id="myDiv"></div>
<div id="myDiv2"></div>
<script>
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/pt_BR/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-post" data-href="https://www.facebook.com/michael/posts/046153"></div>
<script>
$(document).ready(function(){
myLink = "https://www.facebook.com/michael/posts/045847";
var embPost = '<div class="fb-post" data-href="' + myLink + '"></div>';
$("#myDiv2").append(embPost);
myLink = "https://www.facebook.com/michael/posts/045558";
document.getElementById("myButton").onclick = function() {
var embPost = '<div class="fb-post" data-href="' + myLink + '"></div>';
$("#myDiv").append(embPost);
}
});
</script>
</body>
The click of this button inserts this embPost
in #myDiv
, but does not generate the Embedded post, as expected, on the other hand the #myDiv2
presents the embPost
fully rendered.
Is this just a snippet of code? The variable myLink is being raised where?
– Luiz Picolo
The myLink variable comes from a recovered list via FB.API, asynchronously. I put a.log console to make sure that your content is correct and, in fact, that’s not the problem.
– Alexandre Bonfá
The
myLink
it is generated dynamically?– Luiz Picolo
Yes. The result comes from a FQL, via facebook Graph. This code is inside a loop to create the Embedded one by one...
– Alexandre Bonfá
Yes, but the variable
myLink
contains several embedded? Because if this code is inside a Loop, then the variables are rewritten. I’m not sure I understand very well. I believe more snippets of code could help.– Luiz Picolo
There’s no point in turning this into a loop. The information in the question is few, and the code has probably been edited leaving something important out.
– J. Bruni
I put all the code. I left the loop out because it is not important for this study.
– Alexandre Bonfá
You’re testing it wrong. The code you posted now requires "onReady" or "setTimeout" or some way to wait for the Facebook script to load and run.
– J. Bruni
J. Bruni, already inserted the "onReady", according to his guidance.
– Alexandre Bonfá
Right... now change it
$(document).ready(function(){
for$('#myButton').click(function(){
in your question - only so that the rest of the text of the question remains coherent without you having to rewrite everything (because it speaks of "click on the button"). ;-) Ok?– J. Bruni