我试图在CKEditor中编辑默认的"link"插件,以便该插件将显示我存储在MySQL数据库中的链接选择。
我已经从插件的锚选择器部分提取了代码,并将其复制到我的"article"片段。
我遇到的问题是,我不知道如何在插件的范围内从php(这是一个JSON数组)获得数据,如下所示。
源:setup: function (a) {
this.clear(); //Clears the dropdown menu
this.add("");
var data; //Initialize the variable data where I want the JSON from PHP to go.
//This is where I want to obtain the data and spit it out!!
$.post("titles.php", function (result) {
//A Data-handler function call here doesn't work?
//data = result also won't work because of scope
//Something has to happen here though...
});
data = JSON.parse(data);
for (var i = 0; i < data.length; i++) {
data[i]["title"] && this.add(data[i]["title"]); //Add each item to dropdown
}
a.article && this.setValue(a.article.name);
(a = this.getDialog().getContentElement("info", "linkType")) && "email" == a.getValue() && this.focus()
}
旁注:如果我将PHP文件的确切输出硬编码到一个变量中,则代码可以工作。
我可能错过了一些愚蠢的东西,比如JS中众所周知的"最佳实践",因为JS不是我的强项,所以如果我无知或愚蠢,请告诉我!
提前感谢!
header.php
<script src="ckeditor.js"></script>
<script>
var Globalvars = {
myvar: '<?php echo $myvar; ?>'
}
</script>
现在,您可以在.js文件中使用这个myvar变量:
alert(Globalvars.myvar);