简体   繁体   English

在TinyMCE iframe中加载jQuery文件

[英]Load jQuery file in TinyMCE iframe

I'm trying to run my site's script.js main javascript file inside my tinyMCE instance. 我正在尝试在我的tinyMCE实例中运行网站的script.js主javascript文件。 The idea is to have functionality, such as my sites slideshow, also running inside tinyMCE for optimal realistic editing. 这个想法是为了使功能(例如我的网站幻灯片)也可以在tinyMCE内运行,以实现最佳的逼真编辑。

I'm not even sure this is possible how I intend it. 我什至不确定这是否可能是我的打算。

Here's the function I use to initialize: 这是我用来初始化的函数:

function set_html_tinymce() {
    tinymce.init({
        selector:'#edit_html_textarea',
        ... ...
    });

    // LOAD jQUERY AND SCRIPT.JS INSIDE TINYMCE
    var scriptLoader = new tinymce.dom.ScriptLoader();
    scriptLoader.add('http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js');
    scriptLoader.add('../js/script.js');
    scriptLoader.loadQueue(function() {
       alert('All scripts are now loaded.');
    });
}

Here's the contents of my test script.js : 这是我的测试script.js的内容:

$( document ).ready(function() {

    alert('a');

    $('.slideshow').on('click', function() {
        alert('b');
    });

    $('.slideshow').click(function() {
        alert('c');
    });

});

I get the first alert a and All scripts are now loaded. 我收到第一个警报a 并且 All scripts are now loaded. , so the script.js is indeed loaded. ,因此script.js确实已加载。 However when I click on a .slideshow class div inside tinyMCE I get neither b nor c alert. 但是,当我在tinyMCE中单击.slideshowdiv ,我既不会收到b警报,也不会收到c警报。

How can I load and run a jQuery/JS file inside tinyMCE, and have it execute in the same manner it would on the site itself? 如何在tinyMCE中加载和运行jQuery / JS文件,并使其以与站点本身相同的方式执行?

I would love an officially supported solution but if this isn't possible I will settle for a 'hack'. 我希望有一个官方支持的解决方案,但是如果这不可能,我将接受“ hack”。

the problem is that TinyMCE uses designMode, so in order to add a js file to the iframe, you must first turn off designMode. 问题在于TinyMCE使用了designMode,因此,要将js文件添加到iframe中,您必须先关闭designMode。

This solution worked for me: 此解决方案为我工作:

var editor = $(tinymce.activeEditor.getBody())[0];
var iframe = $R('txtTinyMCE_ifr');

editor.designMode = 'off';

var script = iframe.contentDocument.createElement('script');
script.setAttribute('src', '../../scripts/tinymce/iframe.js');
editor.appendChild(script);

editor.designMode = 'on';

You should add this code to the init_instance_callback function for TinyMCE. 您应该将此代码添加到TinyMCE的init_instance_callback函数中。

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

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