简体   繁体   English

如何推迟对tinyMCE所见即所得文本编辑器javascript文件的解析(又称为动态加载)?

[英]How can I defer parsing of (a.k.a. dynamically load) the tinyMCE wysiwyg text editor javascript file?

I know how to defer parsing of javascript files . 我知道如何推迟javascript文件的解析 That's easy. 这很容易。

What's not so easy is dealing with the various otherfiles tinyMCE calls after the initial javascript file. 并不是很容易的是在初始javascript文件之后处理tinyMCE调用的各种其他文件。 Mainly because tinyMCE silently fails I suspect. 我怀疑主要是因为tinyMCE默默地失败了。

If I load the file in via a regular html script tag, it works fine. 如果我通过常规的html脚本标记加载文件,则可以正常工作。 But if I try to do something like this: 但是,如果我尝试执行以下操作:

var my_local_tinymce_js_file = "http://domain.com/inc/tinymce/jscripts/tiny_mce/tiny_mce.js";

$.when($.getScript(var my_local_tinymce_js_file)).
done(function(script, textStatus) {

    alert("download of tiny_mce succeeded!");
    setTimeout("setup_tinymce();", 7500);

})
.fail() {

    alert("download of tiny_mce failed!");

});

I can always see the text alert "download of tiny_mce succeeded!", and I can print out the script variable in an alert box as well to prove that it did indeed download the script. 我总是可以看到文本警报“ tiny_mce下载成功!”,并且可以在警报框中打印脚本变量,以证明它确实下载了脚本。

I have even gone as far as calling setup_tinymce 7.5 seconds in the future, to allow for all of the other javascript and css files to load (which I do not believe are loading). 我什至在将来甚至会打电话给setup_tinymce 7.5秒,以允许加载所有其他javascript和css文件(我不相信正在加载)。

The setup_tinymce function looks like this: setup_tinymce函数如下所示:

function setup_tinymce() {

            tinyMCE.baseURL = 'http://domain.com/inc/tinymce/jscripts/tiny_mce/';
            tinyMCE.init({
                mode : "textareas", 
                theme : "advanced", 
                schema: "html5", 
                plugins : "emotions,spellchecker,advhr,insertdatetime,preview", 
                theme_advanced_buttons1 : "bold,italic,underline,|,forecolor,backcolor,|,justifyleft,justifycenter,justifyright,|,fontselect,fontsizeselect", 
                theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,|,image,|,advhr,|,removeformat", 
                theme_advanced_buttons3 : "", 
                theme_advanced_toolbar_location : "top", 
                theme_advanced_toolbar_align : "left", 
                theme_advanced_resizing : true
            });
            tinyMCE.execCommand('mceAddControl', false, 'comment_text');

}

This is why I am trying to dynamically load the file... https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2Fwww.tinymce.com_2Ftryit_2Ffull.php&mobile=false&rule=DeferParsingJavaScript 这就是为什么我尝试动态加载文件的原因... https://developers.google.com/speed/pagespeed/insights#url=http_3A_2F_2Fwww.tinymce.com_2Ftryit_2Ffull.php&mobile=false&rule=DeferParsingJavaScript

It is rather large, and would speed up my page rendering speed considerably if I could dynamically load it. 它相当大,如果我可以动态加载它,它将大大提高我的页面渲染速度。

EDIT: I have even tried some jQuery code like this: 编辑:我什至尝试了一些这样的jQuery代码:

$.when(
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/tiny_mce.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/langs/en.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/plugins/emotions/editor_plugin.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/themes/advanced/editor_template.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/plugins/spellchecker/editor_plugin.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/plugins/advhr/editor_plugin.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/plugins/insertdatetime/editor_plugin.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/plugins/preview/editor_plugin.js"),
        $.getScript("http://domain.com/inc/tinymce/jscripts/tiny_mce/themes/advanced/langs/en.js")
    ).then(


        $(function() {

            //initialize tinyMCE here

        })

    );

With no luck. 没有运气。 That script loads all of the appropriate JavaScript files (that are loaded when I load the script from the head tag) and then initializes tinymce but does not work. 该脚本加载了所有适当的JavaScript文件(当我从head标签加载脚本时加载了这些文件),然后初始化tinymce但不起作用。 No errors are thrown. 没有引发任何错误。

Here are some old forum threads mentioning dynamic loading on the tinymce forum... All of which contain bits and pieces of old code, but no complete examples :S 这是一些旧的论坛线程,其中提到了tinymce论坛上的动态加载...所有内容都包含一点点的旧代码,但没有完整的示例:S

http://www.tinymce.com/forum/viewtopic.php?id=23286 http://www.tinymce.com/forum/viewtopic.php?id=23286

http://www.tinymce.com/forum/viewtopic.php?id=2290 http://www.tinymce.com/forum/viewtopic.php?id=2290

http://www.tinymce.com/forum/viewtopic.php?id=17467 http://www.tinymce.com/forum/viewtopic.php?id=17467

To load tinymce like this might be impossible because the browser does not know how to get all the other needed tinymce files (in case you find a solution (what i do not think you will) let me know - this is of interest to me). 像这样加载tinymce可能是不可能的,因为浏览器不知道如何获取所有其他需要的tinymce文件(以防万一,您找到了解决方案(我认为您不会),请告诉我-这对我很感兴趣) 。

If your aim is to speed up your page with tinymce you may also use the tinymce compressor (see the developer download). 如果您的目标是使用tinymce加快页面速度,那么您也可以使用tinymce压缩程序(请参阅开发人员下载)。 This may save up to 1-2 seconds initially. 最初最多可以节省1-2秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM