Neat Trick However Need Insight -- PHP/JS/Jquery

  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

So I was building a site using PHP and Jquery and I found a need to use PHP inside of a Javascript file to get a Javascript to do a certain function based on the users permission info in PHP. Normally I would have made the file a .php instead of a .js and injected the php in the Javascript where it needed to be.

This would make the Javascript in the file turn to black text in dreamweaver and including it like this would set the content type to a Javascript file
HTML Code: [ Select ]
<script type="text/javascript" src="javascript/custom_functions.php"></script>


As I started to change the file I forgot to remove the .js from the file name and ended up with
HTML Code: [ Select ]
<script type="text/javascript" src="javascript/custom_functions.php.js"></script>


I didn't catch it at first because both PHP and Javascript where working completely fine. This turned the php text to pain black and color coded the file to a Javascript file.

Quick example to test with
JAVASCRIPT Code: [ Select ]
 
// Save this with a .php.js
// test.php.js
 
// Set the message in php
<?php $message = 'This is my test file!'; ?>
 
// Alert the php message in Javascript
alert("<?php echo $message; ?>");
 
 
  1.  
  2. // Save this with a .php.js
  3. // test.php.js
  4.  
  5. // Set the message in php
  6. <?php $message = 'This is my test file!'; ?>
  7.  
  8. // Alert the php message in Javascript
  9. alert("<?php echo $message; ?>");
  10.  
  11.  


So this also works the other way I've found out test.js.php dreamweaver colors it to php instead. So I knew you can't execute PHP in a .js file. which is why this caught me off guard and why I usually use a php file for the instances like this.

though neat I still reverted the code to how I would normally do it but the question I find going through my head is would this double extension naming convention break down over time due to updates in php or is this something that is common practice and will remain?

Since I've not worked with doing double extensions and cannot find anything about them besides uploading them I bring the question here.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I wouldn't depend on this double extension gimmick. It's complicated. More complicated than say, setting up the server to process JS files as PHP.

That said. I don't like to make externally linked JS/CSS files dynamic because it ends up defeating the purpose of my caching efforts.

I load a static JS file like you have there, and include the dynamic stuff in small sections in the HTML file directly under the externally linked script/stylesheet.

The HTML is likely to need reloads anyways. Putting small sections of dynamic script/style in that HTML may look gnarly from a coding perspective, but it ultimately reduces the load on the server since the server doesn't have to load that whole JS file in memory to produce a dynamic version, and it's less the browser needs to load so the page loads faster for the user.
  • ScottG
  • Proficient
  • Proficient
  • ScottG
  • Posts: 477

Post 3+ Months Ago

The project I discovered this on is a site I was building in j query. The happened to be a lot of dynamic information needed based upon the user I also didn't want certain functions or parts of functions visible to those who were not able to use them. The site did a lot of stuff with minimal page refreshes.

I know many ways to complete this task and happened across this and wanted to dive deeper into this. I wasn't planning on going this route but knowing about that's bad practice is better than not knowing knowing at all. Also I've worked on someone Else's project using .php.Inc which I haven't used and if I remember correctly is to try to make the code more transferable across programming languages or something like that. Don't bash me on that I could be wrong on it I've never really looked into it

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 127 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.