简体   繁体   中英

Is it possible to insert SSI into javascript in a PHP file?

As I don't know javascript at all, I'm wondering if it's possible to insert some SSI into javascript in a PHP file. I've got a bit of script going to preload some images, and I'd like to set it up so that some images always get preloaded across my entire site (via the SSI) of which I may add to over time, while allowing me to add individual images as needed page by page. It works fine in my HTML files, but not it my PHP files.

Here's what I've got in my HTML files which works just fine:

<script>
    function preload(arrayOfImages) {
        $(arrayOfImages).each(function () {
            $('<img/>')[0].src = this;
        });
    }
    preload([
        <!--#include virtual="/grabbag/preload-images.html" -->
        '/images/image-1.png',
        '/images/image-2.png'
    ]);
</script>

But this doesn't work in my PHP files, the SSI getting replaced with nothing but empty text:

<script>
    function preload(arrayOfImages) {
        $(arrayOfImages).each(function () {
            $('<img/>')[0].src = this;
        });
    }
    preload([
        <?php include("/grabbag/preload-images.php"); ?>
        '/images/image-1.png',
        '/images/image-2.png'
    ]);
</script>

Is this possible? Thanks in advance.

Your PHP include is absolute. It begins with a / and therefore it'll look in the root (the up most path) and tries to traverse down. Change it to a relative one or the correct absolute one and a it'll work 😉

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