Methods


pasteHTML Method

Pastes HTML text into the given text range, replacing any previous text and HTML elements in the range.

Syntax

object.pasteHTML(sHTMLText)

Parameters

sHTMLText Required. String that specifies the HTML text to paste. The string can contain text and any combination of the HTML tags described in HTML Elements.

Return Value

No return value.

Remarks

This method might alter the HTML text to make it fit the given text range. For example, pasting a table cell into a text range that does not contain a table might cause the method to insert a table element. For predictable results, paste only well-formed HTML text that fits within the given text range.

This method fails only when used inappropriately to paste HTML into a TEXTAREA element in Microsoft Internet Explorer 5 and later.

This method is accessible at run time. If elements are removed at run time, before the closing tag is parsed, areas of the document might not render.

This feature might not be available on non-Microsoft Win32 platforms.

Example

This example uses the pasteHTML method to replace the current selection with a new paragraph.

<SCRIPT LANGUAGE="JScript">
var sel = document.selection;
if (sel!=null) {
    var rng = sel.createRange();
    if (rng!=null)
        rng.pasteHTML("<P><B>Selection has been replaced.</B></P>");
}
</SCRIPT>

Standards Information

There is no public standard that applies to this method.

Applies To

TextRange, TextRange Constructor
Tags :


Community Content

Mr. Raymond Kenneth Petry
Does not replace all HTML tags

This function does *not* replace all HTML tags. It works fine for <B> (and probably also <I> and <U>), but it does not replace links or SPAN tags.

[NB. Does, in contenteditable, MSIE 6-7. - Ray]

Tags :

Thomas Lee
Beware - incompatibility - range, space, endpoint

1. pasteHTML is incompatible with range.text=... which overwrites its node (ie. deleting).

2. pasteHTML(" ... ") suppresses the leading space, but not the trailing.

3. pasteHTML("&nbsp;... ") works-- but &nbsp; tends to revert in Saving File (depending...).

4. pasteHTML affects endpoint ranges despite duplicate (but with-content are not-affected).

5. pasteHTML can reach outside its specified text-range and enclose the whole in <P></P> (if it contains a <P> or <BR><BR>: probably an interaction with the keyboard-data-entry SHIFT/ENTER-<BR>/<P>-decision; The effect is, it pulls the outer-texts into its <P>'s).

6. Note also that it can include &nbsp; to do so....

7. Note that Drag-and-Drop does this too, but pasteHTML from the clipBoard deletes most html, but converting to <BR>/<P>.

Tags : contentbug?

Thomas Lee
Buggy behavior???

1) If you ever need to clear all the contents of your rich-edit text control, a simple way to do it would be by selecting the whole document (body or div) and then apply: textRange.pasteHTML("") , however (on IE7) this only clears the text between the tags, but not the tags themselves.

2) if you ever think of implementing the undo feature by using textRange.select() and textRange.pasteHTML(), may be you should think it twice, becuase textRange.select() may work fine, but textRange.pasteHTML() won't do anything except clearing the selection.

Tags : productbug?

Thomas Lee
How to highlight the text I just paste with pasteHTML?
After t.pasteHTML('...') is executed, the the original text range collapses and places the cursor after the pasted text. I was trying to use range.moveStart('character',-text.length) but when text has HTML characters, like &gt; and &nbsp;, the number of characters it moves back is not correct. Without decodeing these & characters one by one, how to highlight (or select) the text that has just been pasted?

[tfl 10 07 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at

http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. For specific help about:
Visual Studio :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
SQL Server :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.sqlserver%2C&
.NET Framework :
http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&



Mr. Raymond Kenneth Petry
N.B. - no event
Note that this method does not invoke the onpaste event.
Tags :

javruben
How to get a range on pasted html
range.pasteHTML("<div id='insertion'>" + value + "</div>");
var tempNode = document.getElementById("insertion");
range.moveToElementText(tempNode);
tempNode.removeNode();
Tags :

Page view tracker