简体   繁体   中英

Including php file as JS

I am trying to change the image_list_url of tiny_mce to php file.

I changed the url to image_list.php file. It generated the exact output text same as the js file.

But even after giving same output it doesn't show's the image list.

I am wondering if the content-type is affecting it or not?

my JS file content:

// 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"]
);

my PHP COde:

<?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; ?>
);

my PHP Output:

// 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"]
);

Edit:

As per suggestion i even added a popup message too which even didn't show up.

Solution:

dun know what was error on my code but found good solution from link suggested:

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

As both the .js and your PHP file outputs are identical, there should be no difference. text/javascript is the most widely supported mime type for JS, so using that might help.

It would also not hurt to name your dynamically generated JS files using a convention such as XYZ.php.js and using mod_rewrite to parse the php.js files as php.

Edit:

Also, per official TinyMCE docs , please make sure that there is no whitespace before the <?php opening tag in the dynamically generated JS, also check for UTF8 BOM which can be a sneaky cause of invisible output.

No need to change any headers. Just output the JavaScript.

js.php: alert("Working!")

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

When I loaded test.htm, I got an alert box

This is definitely a problem of incorrect header type.

could you please change the line

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

to

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

As application/x-javascript is not a correct javascript header. Tell me if this thing helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM