views:

60

answers:

3

..I suppose because the html has script tags :-/

<script type="text/javascript">
$(document).ready(function() {
     $('.ad_slot').html('<scr'+'ipt type="text/javascript"><!-- amazon_ad_tag = "xxxxxxxx-xx"; amazon_ad_width = "160"; amazon_ad_height = "600"; //--></scr'+'ipt>');        
}); 
</script>

<div class="ad_slot"></div>

without the script tags the html displays fine. Is there any way to make this work with the tags included?

I need to generate a full js code using js for a project I'm working on.

I've also added the code to jsFiddle http://jsfiddle.net/c68wu/ although I'm not sure if scripts will show in the result window.

+1  A: 

Try to escape slash in the closing script tag:

$('.ad_slot').html('...<\/script>'); 
serg
It doesnt matter about the forward slash - I have included many tags in the html() function, and all tags involve a slash to close. Ive never had a problem.
ClarkeyBoy
@ClarkeyBoy you clearly never included `<script>` tag then, because it wouldn't work without escaping slash.
serg
I just put all my script tags at the top - I take note of what **could** appear on the page, and include the jquery which applies to it by default - that way all the scripts stay in one place, where they should be - the Head tag...
ClarkeyBoy
Ok I'll admit you were right... I assumed that rules which apply to one tag also apply to all others. Could you edit your answer (just add a space or something) so that I can up-vote it again?
ClarkeyBoy
@ClarkeyBoy edited...
serg
ta... sorry about the confusion. im always being told "never assume, always check"... maybe I should take a hint from this... lol
ClarkeyBoy
@ClarkeyBoy no problem
serg
A: 

You'll need to escape the forward slashes:

$('.ad_slot').html('<scr'+'ipt type="text\/javascript"><!-- amazon_ad_tag = "xxxxxxxx-xx"; amazon_ad_width = "160"; amazon_ad_height = "600"; \/\/--><\/scr'+'ipt>');

Or this:

$('.ad_slot').html('<script type="text/javascript">amazon_ad_tag = "xxxxxxxx-xx"; amazon_ad_width = "160"; amazon_ad_height = "600";</script>');

I removed the HTML comment tags and the + in script tags. Oh yeah, and I removed the escaping on the slashes...not needed.

Also, you may want to look at when your script to generate the ads is being loaded and run. I suspect it's run before and never sees this code.

Darryl Hein
Thanks! I tried but still no result.
cyberjunkie
I don't think your code will actually do anything since there is nothing to trigger anything. There are just some variables.
Darryl Hein
Sorry, the script is longer, I just posted the part I can;t figure out. Removing the + in script tags causes an unterminated string literal error. Removing the comment tags however is necessary, thanks :)
cyberjunkie
+1  A: 

Script tags are going to be stripped out if you attempt to add them with html. Use jQuery.getScript() instead.

tandu
Thanks! I think you;re on to something! I tried like so `$('.ad_slot').getScript('<scr'+'ipt type="text\/javascr'+'ipt"> amazon_ad_tag = "xxxxxxxxxxxxxx-xxx"; amazon_ad_width = "160"; amazon_ad_height = "600"; <\/scr'+'ipt>');` but I get `$(".ad_slot").getScript is not a function`
cyberjunkie
getScript() is an asynchronous call to a javascript file. You cannot call your script the way you are trying to. Get rid of the script tags and put the amazon_... stuff in its own file, then call getScript() on that file. You are not using getScript correctly anyway. It is $.getScript(url); notice call of getScript on jQuery object; it cannot be applied to a selector.
tandu
Oh I see! Thank you! I'll experiment with it :)
cyberjunkie