0

I'm trying to get the Facebook pixel, containing a big chunk of JS code stored as a variable. This is my code:

$('a').click(function(){


var fbpixel = eval("<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');

fbq('init', '202225353513153');
fbq('track', "PageView");</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=202225353513153&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->");


});

I know the eval function is deprecated, but I've tried other ways too but I just don't get it.

Sweely
  • 386
  • 3
  • 15
  • What are you trying to do exactly? Why does it need to be a string? – 4castle Oct 20 '16 at 21:11
  • I'm trying to get the code block to fire when someone clicks a link. It doesn't have to be a string, I'm just overcomplicating things. – Sweely Oct 20 '16 at 21:16
  • It sounds as though the code should be inside a plain-old function, which you call when the link is clicked. – 4castle Oct 20 '16 at 21:17
  • Thanks, I'll create a function. Would I need to escape the code block somehow in the function? I'll be trying " and ' – Sweely Oct 20 '16 at 21:18
  • No, when the code is part of a function as source code, it doesn't need to be escaped like strings do. – 4castle Oct 20 '16 at 21:23

0 Answers0