labu77 labu77 - 18 days ago 8
HTML Question

Add javascript with jQuery

When a div has zero height, I try to add a javascript code.

HTML:

<div class="med_rec"></div>


jQuery:

if ($(".med_rec").height() === 0) {

$('.med_rec').text('<script type="text/javascript" src="//static.criteo.net/js/ld/publishertag.js"></script>
<script type="text/javascript">
Criteo.DisplayAd({
"zoneid": 11111,
"async": false});
</script>')

}


I tried it with

</' + 'script>


but there is always a syntax error.

Thank you

Answer

Apparently the problem was with : '<' and '>'

Use this:

$('.med_rec').append('\<script type="text/javascript"  src="//static.criteo.net/js/ld/publishertag.js"\>\</script\><script type="text/javascript">Criteo.DisplayAd({"zoneid": 11111,"async": false});\</script\>');

I hope it helps