包括php文件作为JS

时间:2011-04-05 08:04:47

标签: javascript php

我正在尝试将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

3 个答案:

答案 0 :(得分:1)

由于.js和PHP文件输出相同,因此应该没有区别。 text/javascript是JS最广泛支持的mime类型,因此使用它可能有所帮助。

使用XYZ.php.js之类的约定命名动态生成的JS文件并使用mod_rewrite将php.js文件解析为php也没什么坏处。

编辑:

另外,根据官方TinyMCE docs,请确保动态生成的JS中的<?php开始标记之前没有空格,同时检查UTF8 BOM这可能是一个偷偷摸摸的原因看不见的输出。

答案 1 :(得分:0)

无需更改任何标题。只需输出JavaScript。

js.php:alert("Working!")

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

当我加载test.htm时,我收到了一个警告框

答案 2 :(得分:0)

这肯定是标题类型不正确的问题。

请你改变一下

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

标题('Content-type:application / javascript');

由于application / x-javascript不是正确的javascript标头。告诉我这件事是否有帮助