I’m trying to have adsense javascript code added to a designated div location using jquery but it seems javascript code does not sit well inside a jquery variable. It executes. I’ve tried using php’s htmlentities to encode it for storage, but I can’t get it to decode naturally. What should I do? Do I need a javascript based replacement for htmlentities_decode?
This is how far I’ve gotten, and .html() is not automatically decoding the htmlentities encoded html.
var html_1 = "<?php echo htmlentities('<script>adsense ad code here</script>'); ?>";
if (html_1)
{
jQuery('#id_wpt_adblock_1').html(html_1);
}
<script>tags are automatically executed when they are added to the DOM. So, when the string is added viahtml(), the<script>is added to the DOM, and than ran.The issue here, is the
<script>tags in the string. When the browser sees ’em, it may try to run the script. Try to change the string to this:This should output:
Which should work.
EDIT: You said this string is in a variable, try using
str_replaceto replace the tags.