insertAdjacentHTML method (Internet Explorer)

Switch View :
ScriptFree
insertAdjacentHTML method

[This documentation is preliminary and is subject to change.]

Inserts the given HTML text into the element at the location.

Syntax

object.insertAdjacentHTML(where, html)

Standards information

There are no standards that apply here.

Parameters

where [in]

Type: BSTR

String that specifies where to insert the HTML text, using one of the following values:

beforeBegin

Inserts html immediately before the object.

afterBegin

Inserts html after the start of the object but before all other content in the object.

beforeEnd

Inserts html immediately before the end of the object but after all other content in the object.

afterEnd

Inserts html immediately after the end of the object.

html [in]

Type: BSTR

String that specifies the HTML text to insert. The string can be a combination of text and HTML tags. This must be well-formed, valid HTML or this method will fail.

Return value

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Type: HRESULT

If this method succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code.

Remarks

If the text contains HTML tags, the method parses and formats the text as it is inserted.

You cannot insert text while the document is loading. Wait for the onload event to fire before attempting to call this method.

When using the insertAdjacentHTML method to insert script, you must include the DEFER attribute in the script element.

Examples

This example uses the insertAdjacentHTML method to insert script into the page.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/insertscript_1.htm


var sHTML="<input type=button onclick=" + 
    "go2()" + " value='Click Me'><BR>"
var sScript='<SCRIPT DEFER>'
sScript = sScript + 
    'function go2(){ alert("Hello from inserted script.") }'
sScript = sScript + '</script' + '>';
ScriptDiv.insertAdjacentHTML("afterBegin",sHTML + sScript);

See also

a
abbr
acronym
address
applet
area
article
aside
b
bdo
big
blockQuote
body
br
button
caption
center
cite
code
col
colGroup
custom
dd
del
dfn
dir
div
dl
dt
em
embed
fieldSet
figcaption
figure
font
footer
form
frame
frameSet
head
header
hgroup
hn
hr
html
i
iframe
img
input type=button
input type=checkbox
input type=file
input type=hidden
input type=image
input type=password
input type=radio
input type=reset
input type=submit
input type=text
ins
isIndex
kbd
label
legend
li
listing
map
mark
marquee
menu
nav
nextID
noBR
object
ol
optGroup
option
p
plainText
pre
q
rt
ruby
s
samp
section
select
small
span
strike
strong
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
tr
tt
u
ul
var
xmp
Element Constructor
Reference
innerHTML
insertAdjacentText
outerHTML

 

 

Build date: 2/14/2012

Community Content

Terje Rosenlund
Warning

TABLE, THEAD, TBODY, TFOOT and TR and are not mentioned in the Applies To list because insertAdjacentHTML is often invalid for them (odd). Intuitively it would be allowed to insert a complete TBODY in a TABLE, a complete TR to the TBODY etc.

All these tags have insertAdjacentHTML attached to them so it's very impotent to embed this method in a try/catch when used in functions where target and html are passed as parameters to avoid:

2146827688, Invalid target element for this operation.


This limitation is unike to IE and works as expected in: Firefox, Chrome, Safari and Opera

Warning: insertAdjacentHTML() is a valid function if eg. html-fragment is a DIV but it may be invisible and/or cause size and position problems, especially if DIV.style.position = 'absolute'


Thomas Lee
Comment

it's not valid method for TR element