0

I am using kind of this solution to execute <script> that is inserted via innerHTML, but I need to make sure, those scripts are executed only once. So my question is:

Can I be sure that scripts will not be executed when inserted via innerHTML and why?

EDIT: I need a solution where I can put scripts into a div, that will not be executed when inserted. I want to parse these scripts and execute them by another code snippet. I have a solution that is using innerHTML to insert script-tags into a div and it is working good.

What would be the appropiate way to insert scripts into a div without having them executed when inserted?

Community
  • 1
  • 1
Peavey
  • 302
  • 2
  • 11
  • 2
    Avoiding innerHTML solves your problems. innerHTML is a confusing and wrong tool. – Raynos Nov 18 '11 at 12:18
  • 1
    That depends on the situation. It perfectly OK to use `innerHTML` for static contents. Inserting ` – J. K. Nov 18 '11 at 12:20
  • Thanks guys, but that does not answer my question at all... please check my EDIT on the question – Peavey Nov 18 '11 at 14:41
  • Ah, check out my edited answer. I did even actually kind of answer to your problem before. – J. K. Nov 18 '11 at 14:52

1 Answers1

1

If you insert the same script multiple times, it will be executed the corresponding number of times. The DOM parser in the browser executes any <script> element (unless its type is not text/javascript) it encounters.

EDIT: Use a different type than text/javascript—for instance type="text/custom". The parser only executes text/javascript.

J. K.
  • 8,268
  • 1
  • 36
  • 35
  • I cannot edit the javascripts that are being inserted - I'll have to use them as they are. From my experience, scripts that are inserted via innerHTML don't get executed upon insertion, for me this is perfect and as far as I tested it works across all common browers. I want to know if this reliably not working, so I can build my logic on this... – Peavey Nov 18 '11 at 14:59