Print Javascript in Javascript?
NickName:Liam W Ask DateTime:2013-09-15T00:33:47

Print Javascript in Javascript?

I am in the process of making an addon for a software that basically allows you to have 'responsive' adverts, by checking the page size with javascript and then outputting the relevant ad code to the screen.

This works fine for text ad codes, however I've hit a snag when the ad code is javascript - I can't get the user-provided javascript to output to the page and be executed.

Is this possible at all?

Here is some example code:

<div id="admanagerresponsive">
    <script type="text/javascript">
      adUnit = document.getElementById("admanagerresponsive");
      adWidth  = adUnit.offsetWidth;

      if (adWidth >= 728) {
        <output ad code>
      }
    </script>
</div>

The code above will be directly in the page.

Is such a thing possible?

EDIT:

could be any advertiser's code, such as adsense. It'll be user provided, and will be standard html. However, it could contain tags, and these will need to be rendered and outputted correctly...

Copyright Notice:Content Author:「Liam W」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/18803917/print-javascript-in-javascript

Answers
t.niese 2013-09-14T16:55:41

If you really need to inject add html code containing script tags and you are award of the security problems, i suggest to use a library like jQuery that takes care about the cross browser issues with executing <script> tags added later.\n\nAdditionally you need to take care about various pitfalls like:\nHtml paring is done before script parsing, so no matter where a </script> appears this will immediately end your script.\n\nThe examples are important for the situations where you have that code as inline script inside your html page.\n\nExample 1:\n\n<script>\n adUnit = document.getElementById(\"admanagerresponsive\");\n adWidth = adUnit.offsetWidth;if (adWidth >= 728) {\n // if you add </script> <b>this is visible as html</b> and everything below is not script anymore\n }\n</script>\n\n\nExample 2:\n\n<script>\n adUnit = document.getElementById(\"admanagerresponsive\");\n adWidth = adUnit.offsetWidth;if (adWidth >= 728) {\n var string = \"<script> var test;</script>\";//the same problem here everything behind the closing script tag is html and not script anymore\n }\n</script>\n\n\nSo if you need to have some script to inject there you need to make the </script> not to be detectable by the html parser:\n\n<script>\n adUnit = document.getElementById(\"admanagerresponsive\");\n adWidth = adUnit.offsetWidth;if (adWidth >= 728) {\n var string = \"<script> var test;</sc\"+\"ript>\";//that way the html parser does not detect the closing script tag\n }\n</script>\n\n\nA better solution is not to use inline script at all, not only for that reason, but because you should always keep css, js and html separated.",


More about “Print Javascript in Javascript?” related questions

Print using Javascript?

I have particular division in my web page. i want to print that div only when click "print" in my web page. i have some javascript code. But that is not well formatted. Is there any javascript to...

Show Detail

Print pass a file reference to javascript window.print() / print a pdf using javascript

I just want to call the browser's print functionality, but rather than have it print the current page, I want it to print a specific document. Is this possible? I'm aware of how to use things like

Show Detail

javascript print link

as I am new to Javascript you guys might save me some time answering this simple question. How to make Javascript functionality that prints linked document(not the current window). Lets say that ...

Show Detail

Print Function without javascript

I am writing an HTML Email Code. One of the components in the Email Page is the Print Button. But, As we know the Email Clients blocks the javascript and hence I cannot use window.print(); I read t...

Show Detail

JavaScript print preview

How can I see print preview of my invoice generated from website. If I print with the script &lt;a href="javascript:window.print()"&gt;print this page&lt;/a&gt; in the print "print this page" also

Show Detail

Print current stack trace in JavaScript

How do I print a stack trace from JavaScript? The answer How can I get a Javascript stack trace when I throw an exception? deals with throwing an exception, but I need to print stack traces to deb...

Show Detail

JavaScript print for CKEditor content

Html: &lt;textarea name="Editor" class="ckeditor" id="aboutme"&gt;@Model.Content&lt;/textarea&gt; &lt;button type="button" onclick="Print()"&gt;Print&lt;/butto

Show Detail

disabling javascript on HTML print page

Is there a way to disable javascript when a page is printed? I have created print specific stylesheets, but when I print preview the javascript is still interacting with some elements (such as cas...

Show Detail

Print with PHP into Javascript

Code for the analysis of the payment page but does not write the code in javascript codes.. For example (HTML Print): &lt;script&gt; woopra.track('Odeme Sayfasi', { urunSayisi: '', amount:...

Show Detail

Launch javascript [ window:print() ] after page with additional javascript loaded

I have a page that is loading around 10 google analytics charts. I would like to launch a print dialog box once this page fully loads -- all charts / javascript loaded. Unfortunately, at the moment...

Show Detail