簡體   English   中英

包括php文件作為JS

[英]Including php file as JS

我正在嘗試將tiny_mce的image_list_url更改為php文件。

我將網址更改為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

由於.js和您的PHP文件輸出相同,因此應該沒有區別。 text/javascript是JS支持最廣泛的mime類型,因此使用它可能會有所幫助。

使用諸如XYZ.php.js之類的約定並使用mod_rewrite將php.js文件解析為php來命名動態生成的JS文件也不會受到損害。

編輯:

另外,根據TinyMCE官方文檔 ,請確保在動態生成的JS中<?php開頭標記之前沒有空格,還要檢查UTF8 BOM ,這可能是不可見輸出的偷偷摸摸的原因。

無需更改任何標題。 只需輸出JavaScript。

js.php: alert("Working!")

test.htm: <script type="text/javascript" src="js.php"></script>

加載test.htm時,出現一個警告框

這絕對是標題類型不正確的問題。

你能換線嗎

header('Content-type:application / x-javascript');

header('Content-type:application / javascript');

由於application / x-javascript不是正確的javascript標頭。 告訴我這件事有沒有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM