15

I'm working with a plugin that is only Javascript. I need to have it dynamically create a DIV element with an advertisement in it.

I can't figure out why this doesn't work:

$(this).append('<div class="overlay-background">Advertisement

     <script type="text-javascript">

          GA_googleFillSlot("blog_landing_right_rectangle_300x250");

     </script>'

It results in the element created with "Hello World" but it does not execute the GA-googleFillSlot function.

1
  • 1
    Do you use this code in the HTML page? if so, closing </script> could be treated as the closing tag from your outermost </script> Commented Jan 11, 2013 at 0:14

6 Answers 6

15

appending HTML into the DOM does not cause the browser to evaluate any script tags in said appended HTML.

If you really wanted to, you could evaluate the javascript by using eval():

eval($(this).find("script").text());
Sign up to request clarification or add additional context in comments.

2 Comments

This solution causes an unknown white screen error when placed with $(this).append. It produces no change when placed outside of it. I did not modify the code from how it appears above.
Looks like dozens of bad requests on pubads.g.doubleclick.net. It looks like I'm appending in a loop (NextGEN gallery carousel).
15

I know this is an old question but I've had a similar problem today. The solution was using createContextualFragment.

My code looks something like this:

var tagString = '<script async type="text/javascript" src="path_to_script"></script>';

var range = document.createRange();
range.selectNode(document.getElementsByTagName("BODY")[0]);
var documentFragment = range.createContextualFragment(tagString);
document.body.appendChild(documentFragment);

Comments

5

This code works in my browser.

$('body').append('<script>alert("test");<' + '/' + 'script>');

so it might be that $(this) is what is actually causing your problem.

Can you replace it with 'body' and see if it works like that?

2 Comments

This works for me, if you break the </script> in the string ...</' + 'script>');
Thanks, I forgot about the script into script, since I usually put 'em into files.
2

One workaround in your case could be to append a fake image with an onload event:

<img src="blank.gif" onload="GA_googleFillSlot('blog_landing_right_rectangle_300x250')" />

Comments

1

since your are in javascript, you can append and then execute the code:

$(this).append('<div class="overlay-background">Advertisement</div>');

GA_googleFillSlot("blog_landing_right_rectangle_300x250");

Comments

0

Please try:

s = '<' + 'script type="text-javascript">' +
    'GA_googleFillSlot("blog_landing_right_rectangle_300x250");' +
    '<' + '/' + 'script>';
$(this).append(s);

UPD: alas, this won't work, please use wless1's solution instead

1 Comment

This won't help as the script won't run anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.