views:

1274

answers:

2

I'm trying to manually clean the HTML of a Telerik RadEditor with Javascript but I can't seem to find the correct place to store the value so that it gets saved on post back.

Here's the JS I have:

$(function () {    
    jQuery.fixHash = function ($html) {      

        // modify $html

        return $html;
    };

    $("#adminEditingArea input[id$='SaveButton']").unbind("click").click(function () {
        $("iframe[id$='_contentIframe']").trigger("save");

        // call .net postback

        return false;
    });

});

var editorSaveEventInit = false;
function InitSaveEvent() {
    if (!editorSaveEventInit) {
        var $EditFrames = $("iframe[id$='_contentIframe']");
        if ($EditFrames && $EditFrames.length > 0) {
            $EditFrames.bind("save", function (e) {
                var $thisFrame = $(this);
                var thisFrameContents = $thisFrame.contents();
                if (thisFrameContents) {
                    var telerikContentIFrame = thisFrameContents.get(0);
                    var $body = $("body", telerikContentIFrame);
                    var html = $.fixHash($body).html();
                    $body.html(html);

                    // also tried storing the modified HTML in the textarea, but it doesn't seem to save:
                    //$thisFrame.prev("textarea").html(encodeURIComponent("<body>" + html + "</body>"));
                }
            });
            editorSaveEventInit = true;
        }
    }
};

$(window).load(function () {
    InitSaveEvent();
});

Is there any way to access the Telerik RadEditor object with JavaScript (using OnClientCommandExecuted()?) so that I can access the .get_html() and .set_html(value) functions? If not, what values do I need to set before posting back?

A: 

Ah, just discovered Telerik's built-in $find() function: http://www.telerik.com/help/aspnet-ajax/editor_getingreferencetoradeditor.html

Edit: here's the solution I came up with for my InitSaveEvent() function:

var editorSaveEventInit = false;
function InitSaveEvent() {
    if (!editorSaveEventInit) {
        var $EditFrames = $("iframe[id$='_contentIframe']");
        if ($EditFrames && $EditFrames.length > 0) {
            $EditFrames.bind("save", function (e) {
                var $thisFrame = $(this);
                var thisFrameContents = $thisFrame.contents();
                if (thisFrameContents) {
                    var telerikContentIFrame = thisFrameContents.get(0);
                    var $body = $("body", telerikContentIFrame);
                    var html = $.fixHash($body).html();
                    // SOLUTION!
                    var $radeditor = $thisFrame.parents("div.RadEditor.Telerik:eq(0)");
                    var editor = $find($radeditor.attr("id"));
                    editor.set_html(html);
                    // ☺
                }
            });
            editorSaveEventInit = true;
        }
    }
};
travis
+3  A: 

Why don't you use custom content filters.

W3Max
Awesome, I had no idea those were even possible. I think it might be a bit late to refactor my current solution, but that will definitely come in handy in the future, thanks!
travis