首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将php文件包含为JS

问将php文件包含为JS
EN

Stack Overflow用户
提问于 2011-04-05 16:04:47
回答 3查看 2K关注 0票数 1

我正在尝试将tiny_mce的image_list_url更改为php文件。

我把url改成了image_list.php文件。它生成与js文件完全相同的输出文本。

但即使在给出相同的输出后,它也不会显示图像列表。

我想知道内容类型是否会影响它?

我的JS文件内容:

代码语言:javascript
复制
// 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 COde:

代码语言:javascript
复制
<?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输出:

代码语言:javascript
复制
// 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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-04-05 16:13:30

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

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

编辑:

此外,对于官方TinyMCE docs,请确保动态生成的JS中的<?php开始标记之前没有空格,还要检查UTF8 BOM,这可能是导致不可见输出的秘密原因。

票数 1
EN

Stack Overflow用户

发布于 2011-04-05 16:15:06

不需要更改任何标头。只要输出JavaScript即可。

js.php:alert("Working!")

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

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

票数 0
EN

Stack Overflow用户

发布于 2011-04-05 16:15:28

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

你能换一下线路吗?

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

至

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

因为application/x-javascript不是正确的javascript标头。告诉我这东西有没有帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5548947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档