Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33427

Re: inner outer html and appendchild

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: inner outer html and appendchild
Date 2017-05-29 19:04 +0200
Organization PointedEars Software (PES)
Message-ID <5274343.lOV4Wx5bFT@PointedEars.de> (permalink)
References <4N-dnWxtPtO7I7bEnZ2dnUU7-KPNnZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> I know about the differences between innerHTML and appendChild but I'm
> in a bit of a bind. If the uses passes a valid html element as a string
> how do I add it to the document without needing to parse the sting or
> contain it in a DIV?
>  
> For example, the users passes (the dots are for brevity)
>    str = "<img id='.' src='..jpg' alt='.' style='.' >";
> or
>    str = "<div id='.' style='.'><p id='.' style='.'>Text <span
> style='.'></span></p></div";

  document.body.innerHTML += str;

is the most compatible, straightforward solution.

But document.body.insertAdjacentHTML(), as Martin suggested, is probably the 
more efficient one.

That said, you SHOULD NOT allow the user to pass HTML in the first place; 
that is a big security risk as it allows Cross-Site Scripting (XSS) attacks.

<https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)>
 
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

inner outer html and appendchild Andrew Poulos <ap_prog@hotmail.com> - 2017-05-29 16:22 +1000
  Re: inner outer html and appendchild Julio Di Egidio <julio@diegidio.name> - 2017-05-29 01:30 -0700
  Re: inner outer html and appendchild Martin Honnen <martin.honnen@gmx.de> - 2017-05-29 11:28 +0200
    Re: inner outer html and appendchild Andrew Poulos <ap_prog@hotmail.com> - 2017-05-29 20:21 +1000
      Re: inner outer html and appendchild Martin Honnen <martin.honnen@gmx.de> - 2017-05-29 12:25 +0200
  Re: inner outer html and appendchild Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-29 19:04 +0200
    Re: inner outer html and appendchild Julio Di Egidio <julio@diegidio.name> - 2017-05-29 10:20 -0700
      Re: inner outer html and appendchild Andrew Poulos <ap_prog@hotmail.com> - 2017-05-30 07:40 +1000
        Re: inner outer html and appendchild Julio Di Egidio <julio@diegidio.name> - 2017-05-29 22:32 -0700
          Re: inner outer html and appendchild Andrew Poulos <ap_prog@hotmail.com> - 2017-05-30 17:29 +1000
            Re: inner outer html and appendchild Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-30 13:13 +0200
              Re: inner outer html and appendchild Tim Streater <timstreater@greenbee.net> - 2017-05-30 12:34 +0100
            Re: inner outer html and appendchild Julio Di Egidio <julio@diegidio.name> - 2017-05-30 05:28 -0700

csiph-web