I’m calling the following function inside of a link’s onClick attribute. There are two such links on the page. When I click one of the links. Everything works fine. When I click the other link, I get an “unexpected identifier error” from the other function.
According to Chrome’s console. The error is “Uncaught Syntax Error: Unexpected identifier.” and it originates from the function that I didn’t click, at the line indicated by the comments in the code below.
Why is this happening?
<a href="#" onClick="
FB.ui({
method: 'send',
link: 'http://www.mydomain.com',
show_error: true,
display: 'popup',
},
function (response) {
//ERROR IS HERE
console.log( 'After FB.ui()' );
jQuery.post(
'<?php echo admin_url( 'admin-ajax.php' ); ?>',
{
action : 'fb_invite_milestone',
postID : MyAjax.postID
}
);
}
);
">LINK</a>
Update:
SOLVED! …but I’m embarrassed to say why. Read my answer below to see how stupid I am.
Oh no!! TOTALLY STUPID MISTAKE: The PHP block inside the callback function was breaking the js syntax because in one instance the function was already inside
<?php ?>and in the other it was not. I missed it because the syntax highlighting was confused by the fact that it was inside a string.