0

I have a .js file that has a nice slide show layout and here is some of the code for it:

slide_links:'false',//Individual links for each slide (Options: false, 'num', 'name', 'blank')
    slides:[{image:'images/2.jpg', title:'Image Credit: John Doe'},
            {image:'images/3.jpg', title:'Image Credit: John Doe'},
            {image:'images/4.jpg', title:'Image Credit: John Doe'},  
            {image:'images/5.jpg', title:'Image Credit: John Doe'},
            {image:'images/6.jpg', title:'Image Credit: John Doe'}] 
});

});

I am wondering could I put a line of php like this in place of ONE of the 'images/2.jpg' lines to showcase a Group of images?

3
  • Its doable yes, but unless its named .php it likely wont execute. Commented Jun 26, 2013 at 18:40
  • you say unless "its" named dot php? what do you mean? like image: file.php? Commented Jun 26, 2013 at 18:44
  • Keep in mind that a .php echo is executed as the page is processed by the server, generating your .js file then the contents are sent to a browser. Commented Jun 26, 2013 at 18:44

4 Answers 4

3

Possible workarounds:

  • use an htaccess rule to run PHP inside JS files like so:

    AddType application/x-httpd-php .js    
    AddHandler x-httpd-php5 .js
    
    <FilesMatch "\.(js|php)$">
    SetHandler application/x-httpd-php
    </FilesMatch>
    
  • make the script inline to some php file

  • put the data in a variable through an inline element in the PHP, and have the external static .js file pick it up.
  • Save the file as .php instead of .js and:

In your PHP file:

Header("content-type: application/javascript");

And then link your file as:

<script type="text/javascript" src="youfile.php"></script>
Sign up to request clarification or add additional context in comments.

Comments

1

Well, Javascript is just text before it's read by the browser. PHP can print text, so the answer is yes. The question is largely dependant on what you can modify, though. If you're defining your slideshow in the JS file, as shown here, it might be a bit more diffuclt. But if you're just defining the slideshow code in the JS file, and declaring the "slides" object in an inline script, it becomes easier for PHP to change that. It's not quite the question you were asking, but it's just a possible design suggestion that might be a bit cleaner. A non-specific example of how you might do it to give you an idea:

<script src="lib_slideshow.js"></script>
<script type="text/javascript">
    var slides = [{image:'images/2.jpg', title:'Image Credit: John Doe'},
    <?php
        echo "{image:'images/customPHPDeterminedImage.jpg', title: 'Image Credit: PHP'},";
    ?>
            {image:'images/3.jpg', title:'Image Credit: John Doe'},
            {image:'images/4.jpg', title:'Image Credit: John Doe'},  
            {image:'images/5.jpg', title:'Image Credit: John Doe'},
            {image:'images/6.jpg', title:'Image Credit: John Doe'}];
    libSlideshow.setup(slides);
</script>

There's probably another possible method involving rewriting the JS through a PHP file, possibly by intercepting the request by using a .htaccess file, but at that point I feel like it would be getting messy.

1 Comment

I see, thanks alot everyone. I will try this and if it fails, find an alternative route
1

You can do that by:

  1. executing a php script that generates your js script
  2. making an ajax call to get the info you want to use on your js

Comments

1

You can indeed but you will have to name the file .php.

You can then run the code and change the output to JavaScript with Content-type.

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

3 Comments

Added a plus-1 to each downvoted answer except my own...something tells me user2019979 was confused about each of the arrows, because none of these answers really seem to be off the mark.
+1 to your answer @Katana314 (and everyone else's). You might also need to setup some extra cache for the .php javascript file.
@DaveChen cheerio, you could do so if there were more then one file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.