1

..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.

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
Oh I see! Thank you! I'll experiment with it :)
1

Try to escape slash in the closing script tag:

$('.ad_slot').html('...<\/script>'); 

5 Comments

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 you clearly never included <script> tag then, because it wouldn't work without escaping slash.
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...
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?
ta... sorry about the confusion. im always being told "never assume, always check"... maybe I should take a hint from this... lol
0

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.

2 Comments

I don't think your code will actually do anything since there is nothing to trigger anything. There are just some variables.
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 :)

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.