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?
-
Its doable yes, but unless its named .php it likely wont execute.datasage– datasage2013年06月26日 18:40:35 +00:00Commented Jun 26, 2013 at 18:40
-
you say unless "its" named dot php? what do you mean? like image: file.php?user2019979– user20199792013年06月26日 18:44:39 +00:00Commented 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.Chad Cook– Chad Cook2013年06月26日 18:44:58 +00:00Commented Jun 26, 2013 at 18:44
4 Answers 4
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>
Comments
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
You can do that by:
- executing a php script that generates your js script
- making an ajax call to get the info you want to use on your js
Comments
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');