2

我试图让 CKEditor.NETCKEditor在页面加载时将一些文本插入到我的页面中。

    Private Sub CKEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myCKEditor.Load

        Dim insertScript As String
        insertScript = "<script type=""text/javascript"">window.CKEDITOR.instances.myWidget_myCKEditor.insertText(""test"");</script>"

        Dim cManager As ClientScriptManager = Parent.Page.ClientScript

        cManager.RegisterStartupScript(Me.GetType, "insertText", insertScript)

    End Sub

我的页面组织如下:

  • myPage(aspx)
    • myWidget(ascx)
      • myCKEditor(来自大会)

我收到以下错误:

TypeError: Cannot read property 'instances' of undefined

调用它时,它似乎CKEDITOR不在身边。如果我打开 JS 控制台并运行insertScript它,它会按需要运行。

我曾尝试将此例程附加到MyBase.LoadMe.Load无济于事,甚至尝试使用window.Load = insertScript,所有这些都会产生相同的错误。

我注意到无论我做什么,源代码中的最后一件事是初始化的脚本CKEditor,即

....
<script type="text/javascript">window.CKEDITOR.instances.myWidget_myCKEditor.insertText("test");</script>
<script type="text/javascript">
//<![CDATA[
window.CKEDITOR_BASEPATH = '/ckeditor/';
//]]>
</script>
<script src="/ckeditor/ckeditor.js?t=C6HH5UF" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var CKEditor_Controls=[],CKEditor_Init=[];function CKEditor_TextBoxEncode(d,e){var f;if(typeof CKEDITOR=='undefined'||typeof CKEDITOR.instances[d]=='undefined'){f=document.getElementById(d);if(f)f.value=f.value.replace(/</g,'&lt;').replace(/>/g,'&gt;');}else{var g=CKEDITOR.instances[d];if(e&&(typeof Page_BlockSubmit=='undefined'||!Page_BlockSubmit)){g.destroy();f=document.getElementById(d);if(f)f.style.visibility='hidden';}else g.updateElement();}};(function(){if(typeof CKEDITOR!='undefined'){var d=document.getElementById('myWidget_myCKEditor');if(d)d.style.visibility='hidden';}var e=function(){var f=CKEditor_Controls,g=CKEditor_Init,h=window.pageLoad,i=function(){for(var j=f.length;j--;){var k=document.getElementById(f[j]);if(k&&k.value&&(k.value.indexOf('<')==-1||k.value.indexOf('>')==-1))k.value=k.value.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');}if(typeof CKEDITOR!='undefined')for(var j=0;j<g.length;j++)g[j].call(this);};window.pageLoad=function(j,k){if(k.get_isPartialLoad())setTimeout(i,0);if(h&&typeof h=='function')h.call(this,j,k);};if(typeof Page_ClientValidate=='function'&&typeof CKEDITOR!='undefined')Page_ClientValidate=CKEDITOR.tools.override(Page_ClientValidate,function(j){return function(){for(var k in CKEDITOR.instances){if(document.getElementById(k))CKEDITOR.instances[k].updateElement();}return j.apply(this,arguments);};});setTimeout(i,0);};if(typeof Sys!='undefined'&&typeof Sys.Application!='undefined')Sys.Application.add_load(e);if(window.addEventListener)window.addEventListener('load',e,false);else if(window.attachEvent)window.attachEvent('onload',e);})();CKEditor_Controls.push('myWidget_myCKEditor');
CKEditor_Init.push(function(){if(typeof CKEDITOR.instances['myWidget_myCKEditor']!='undefined' || !document.getElementById('myWidget_myCKEditor')) return;CKEDITOR.replace('myWidget_myCKEditor',{"htmlEncodeOutput" : true}); });
//]]>
</script>
</form>
 ....

所有的证据都表明我在错误的时间调用了启动脚本,但我不知道调用它的“正确时间”是什么时候。

4

2 回答 2

2

Yoy可以使用jquery windows.ready函数如下

insertScript = "<script type=""text/javascript"">   <script language="javascript">
        $(document).ready(function() {
           window.CKEDITOR.instances.myWidget_myCKEditor.insertText('test');
            })
        })
</script> ";

    Dim cManager As ClientScriptManager = Parent.Page.ClientScript

    cManager.RegisterStartupScript(Me.GetType, "insertText", insertScript) 

它将确保您的 javascript 仅在文档​​准备好而不是 b4 时触发。

于 2013-05-15T06:33:00.753 回答
0

有点逃避,但我最终加载了我需要在页面加载时插入的内容并在.ascx文件中定义脚本。

在我的myWidget.ascx

<script type="text/javascript">
    function ddChange(obj) {
        window.CKEDITOR.instances.myWidget_myCKEditor.insertText(obj.value);
        document.getElementById("myWidget_myCannedPicker").selectedIndex = 0;  
    }
</script>
...
<asp:DropDownList ID="myCannedPicker" Width="400" runat="server" AutoPostBack="false" OnChange="ddChange(this)" /></span></td>

myWidget.ascx.vb

    Private Sub fillInitialValues()

        ...
        myCannedPicker.DataSource = dt
        myCannedPicker.DataValueField = "msg_text"
        myCannedPicker.DataTextField = "msg_name"
        myCannedPicker.DataBind()
        myCannedPicker.Items.Insert(0, New ListItem("Please make a selection:", "0"))

    End Sub

并没有真正回答原始问题,但在我的情况下这是一个合适的解决方法。

于 2013-05-19T12:14:32.480 回答