2011-04-05 140 views
1

我想將tiny_mce的image_list_url更改爲php文件。包括PHP文件作爲JS

我將url更改爲image_list.php文件。它生成與js文件相同的確切輸出文本。

但即使在給出相同的輸出後,它也不會顯示圖像列表。

我想知道內容類型是否影響它?

我的JS文件內容:

// This list may be created by a server logic page PHP/ASP/ASPX/JSP in some backend system. 
// There images will be displayed as a dropdown in all image dialogs if the "external_link_image_url" 
// option is defined in TinyMCE init. 

var tinyMCEImageList = new Array(
    // Name, URL 
    ["Logo 1", "media/logo.jpg"], 
    ["Logo 2 Over", "media/logo_over.jpg"] 
); 

我的PHP代碼:

<?php 
    require('../../../system/config.php'); 
    $strPath = APP_ROOT.DS.'sys_uploads/images/'; 
    $objFileList = dir($strPath); 
    $arrFileList = array(); 
    while (false !== ($entry = $objFileList->read())) { 
     if(is_file($strPath.$entry)) 
      $arrFileList[] = array($entry, ABS_URL.'/sys_uploads/images/'.$entry); 
    } 
    $objFileList->close(); 

    header('Content-type: application/x-javascript'); 
    //header('Content-type: text'); 
?> 
// This list may be created by a server logic page PHP/ASP/ASPX/JSP in some backend system. 
// There images will be displayed as a dropdown in all image dialogs if the "external_link_image_url" 
// option is defined in TinyMCE init. 

var tinyMCEImageList = new Array(
    // Name, URL 
<?php 
    if(count($arrFileList)>0) 
     foreach($arrFileList as $dataRow): 
?> 
    ["<?php echo $dataRow[0];?>", "<?php echo $dataRow[1];?>"], 
<?php endforeach; ?> 
); 

我的PHP輸出:

// This list may be created by a server logic page PHP/ASP/ASPX/JSP in some backend system. 
// There images will be displayed as a dropdown in all image dialogs if the "external_link_image_url" 
// option is defined in TinyMCE init. 

alert('test working or not'); 

var tinyMCEImageList = new Array(
    // Name, URL 
    ["Logo 1", "media/logo.jpg"], 
    ["Logo 2 Over", "media/logo_over.jpg"] 
); 

編輯:

按照建議,我甚至增加這也是一個彈出消息沒有出現。

解決方案:

逼債知道什麼是我的代碼錯誤,但找到很好的解決辦法,從鏈接提示:

http://tinymce.moxiecode.com/wiki.php/Configuration%3aexternal_image_list_url

+0

你爲什麼要計算'$ arrFileList'的大小?只要它是一個數組,它就不是'foreach'所必需的。 – Htbaa 2011-04-05 08:10:52

+0

我想跳過,如果它的空白... thnx的建議...它可以幫助我優化我的代碼 – KoolKabin 2011-04-05 08:13:32

回答

1

同時作爲.js文件和你的PHP文件的輸出是相同的,應該有沒有區別。 text/javascript是JS最廣泛支持的MIME類型,所以使用可能幫助。

它也不會傷害到你的名字動態生成JS文件使用約定,如XYZ.php.js和使用mod_rewrite來解析php.js文件作爲PHP。

編輯:

而且,每官方TinyMCE docs,請確保有動態生成JS的<?php開放標記之前沒有空格,還要檢查其UTF8 BOM可以是無形產出的偷偷摸摸的原因。

+0

我試圖添加標題('Content-type:text/javascript');但它並沒有幫助...以及第二個想法,這也是偉大的...我會嘗試 – KoolKabin 2011-04-05 08:15:18

+0

你如何包括js文件? – 2011-04-05 08:17:53

+0

它只是從tiny_mce腳本...即。在tiny_mce_url列表的選項中指定它... – KoolKabin 2011-04-05 08:19:18

0

無需更換任何頭。只輸出JavaScript。

js.php:alert("Working!")

TEST.HTM:<script type="text/javascript" src="js.php"></script>

當我裝TEST.HTM,我得到了一個警告框

+0

我也試過你的建議......但它不會彈出消息... – KoolKabin 2011-04-05 08:21:29

0

這肯定是不正確頭類型的問題。

請你改變線

報頭( '內容類型:應用程序/ x-的javascript');

報頭( '內容類型:應用/ JavaScript的');

由於application/x-javascript不是正確的JavaScript標頭。告訴我這個東西是否有幫助

+0

我改變了它,但沒有改變... – KoolKabin 2011-04-05 08:21:07