我有一个使用 TinyMCE 表单的网站;独立地,我使用 jQuery。当我从 Firefox 3(MacOS X、Linux)上的登台服务器加载表单时,TinyMCE 没有完成加载。Firefox 控制台出现错误,提示t.getBody()
返回null
. t.getBody()
,据我从 TinyMCE 文档中了解到的,是一个返回文档正文元素以检查某些功能的函数。当我使用 Safari 时不会出现问题,当我使用 Firefox 和从 localhost 运行的同一站点时也不会出现问题。
原始的、失败的 JavaScript 相关代码如下所示:
<script type="text/javascript" src="http://static.alfa.foo.pl/json2.js"></script>
<script type="text/javascript" src="http://static.alfa.foo.pl/jquery.js"></script>
<script type="text/javascript" src="http://static.alfa.foo.pl/jquery.ui.js"></script>
<script type="text/javascript" src="http://static.alfa.foo.pl/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({ mode:"specific_textareas", editor_selector:"mce", theme:"simple", language:"pl" });
</script>
<script type="text/javascript" src="http://static.alfa.foo.pl/jquery.jeditable.js"></script>
<script type="text/javascript" src="http://static.alfa.foo.pl/jquery.tinymce.js"></script>
<script type="text/javascript" charset="utf-8" src="http://static.alfa.foo.pl/foo.js"></script>
<script type="text/javascript">
$(document).ready(function(){
/* jQuery initialization */ });
</script>
我尝试更改脚本加载顺序,移动tinyMCE.init()
call to the
<script/>
tag containing $(document).ready()
call—before, after, and inside this call. No result. When tinyMCE.init()
was called from within
$(document).ready()
handler, the browser did hang on request—looks like it was too late to call the init function.
Then, after googling a bit about using TinyMCE together with jQuery, I changed tinyMCE.init()
call to:
tinyMCE.init({ mode:"none", theme:"simple", language:"pl" });
and added following jQuery call to the $(document).ready()
handler:
$(".mce").each( function(i) { tinyMCE.execCommand("mceAddControl",true,this.id); });
调用,发出警报,并且 TinyMCE 文本区域已正确初始化。我认为这可能是由于等待用户解除警报而引入的延迟问题,因此我通过将仍在 $(document).ready() 处理程序中的调用更改为以下内容来引入一秒钟的延迟:Still the same error. But, and here's where things start to look like real voodoo, when I added alert(i);
before the tinyMCE.execCommand()
setTimeout('$(".mce").each(function(i) { tinyMCE.execCommand("mceAddControl",true,this.id); });',1000);
随着超时,TinyMCE textareas 正确初始化,但它是围绕真正问题的管道胶带。这个问题看起来像是一个明显的竞争条件(特别是当我考虑在同一个浏览器上,但当服务器在本地主机上时,问题不会发生)。但是 JavaScript 执行不是单线程的吗?任何人都可以告诉我这里发生了什么,实际问题在哪里,以及我能做些什么来真正解决它?