Re: Obtaining Selected Image HTML

  •  04-17-2011, 9:22 PM

    Re: Obtaining Selected Image HTML

    Hi BinbinB,
     
    Please try the example below
     
    <%@ Page Language="C#" AutoEventWireup="true" %>

    <%@ Register TagPrefix="CE" Namespace="CuteEditor" Assembly="CuteEditor" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <CE:Editor ID="editor1" runat="server">
            </CE:Editor>
            <input type="button" value="Get image html code" onclick="getSrc()" />
        </form>
    </body>
    </html>

    <script>
    function getSrc() {
        alert(getSelectedHTML());
    }


    function getSelectedHTML() {
        var editor1 = document.getElementById("<%= editor1.ClientID %>");
        var editwin = editor1.GetWindow();
        var editdoc = editor1.GetDocument();
        var rng = null,
            html = "";
        if (document.selection && document.selection.createRange) {
            rng = editdoc.selection.createRange();
            if (editdoc.selection.type == "Control") {
                if (rng.length == 1) {
                    var element = rng.item(0);
                    if (element.tagName == 'IMG') {
                        html = element.src;
                        //get html code of img tag
                        //html=element.outerHTML;

                    }
                }
            } else {
                html = rng.htmlText || "";
            }
        } else if (window.getSelection) {
            rng = editwin.getSelection();

            if (rng.rangeCount > 0 && window.XMLSerializer) {
                rng = rng.getRangeAt(0);
                for (var i = 0; i < rng.startContainer.childNodes.length; i++) {
                    if (rng.startContainer.childNodes[i].tagName == "IMG") {

                        html = rng.startContainer.childNodes[i].src;
                    }

                }

            }
        }
        return html;
    }
    </script>
     
    Regards,
     
    Ken
View Complete Thread