Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I separate javascript from PHP when the JS needs a PHP variable?

Below is a small snippet from a code I saw with jquery and PHP.

Notice the PHP part on line 5, I generally put my javascript into separate files so how would I be able to keep my JS in separate files but still use PHP when needed like below?

//when the DOM is ready  
$(document).ready(function(){  
    //settings on top  
    var domain = 'http://davidwalsh.name/';  
    var initialPosts = <?php echo get_posts(0,$_SESSION['posts_start']); ?>;  
    //function that creates posts  
    var postHandler = function(postsJSON) {  
        $.each(postsJSON,function(i,post) {  
like image 445
JasonDavis Avatar asked Aug 22 '09 15:08

JasonDavis


People also ask

Can we assign JavaScript variable to PHP variable?

Javascript will be interpreted in Client's browser and you can not assign it to PHP variable which is interpreted on SERVER .

Can you mix PHP and JavaScript?

Besides, PHP and JavaScript similarities, these two languages are a powerful combination when used together. Large numbers of websites combine PHP and JavaScript – JavaScript for front-end and PHP for back-end as they offer much community support, various libraries, as well as a vast codebase of frameworks.

Should I separate JavaScript files?

You should put your JS code in a separate file because this makes it easier to test and develop. The question of how you serve the code is a different matter. Serving the HTML and the JS separately has the advantage that a client can cache the JS.

Does JavaScript run before PHP?

PHP is executed on the web-server, generating an html (or whatever) page to be displayed by the browser. JavaScript is executed on the client-side, in the user's browser, therefore php has, by definition, finished executing before JavaScript executes.


2 Answers

What I generally do is :

  • put as much JS as possible in a .js file (for caching on the client-side and all that)
  • this JS code uses a JS variable
  • that JS variable is declared / initialized from a PHP file ; this is the only part where you need some code executed on the server-side, actually

For instance, I would have something like this, I suppose :

my-file.php :

var thisIsMyJSVar = '<?php echo $test; ?>';

So, in PHP, we declare tha variable and set its value. This is the "dynamic" part.

and, in my-file.js :

//when the DOM is ready  
$(document).ready(function(){  
    //settings on top  
    var domain = 'http://davidwalsh.name/';  
    var initialPosts = thisIsMyJSVar;  // Use the JS variable declared in the PHP file
    //function that creates posts  
    var postHandler = function(postsJSON) {  
        $.each(postsJSON,function(i,post) {

Here, in the static JS file, we only use the value ; nothing here is dynamic, and this file can be cached by the client -- to not be re-downloaded on each page.


The problem with that idea is the JS file depends on some initialisation done in the PHP file :-(

So, it might be a good idea to have a "default value" in the JS file, just in case...


Also, you have to have a good namming convention, to not have several files using/declaring/depending on the same JS variable ; it might be a good idea, actually, to put all your "configuration variables" inside a single javascript object, to not pollute the global namespace...

like image 128
Pascal MARTIN Avatar answered Sep 21 '22 15:09

Pascal MARTIN


If you want to keep your Javascript separate from your PHP, then use a PHP file to generate a small chunk of Javascript just to set variables. Then use those variables from your .js file:

HTML:

<script>
var INITIAL_POSTS = <?php echo get_posts(0,$_SESSION['posts_start']); ?>;
</script>
<script src="my.js">

my.js Javascript file:

//when the DOM is ready  
$(document).ready(function(){  
    //settings on top  
    var domain = 'http://davidwalsh.name/';  
    var initialPosts = INITIAL_POSTS;  
    //function that creates posts  
    var postHandler = function(postsJSON) {  
        $.each(postsJSON,function(i,post) {
like image 35
Ned Batchelder Avatar answered Sep 20 '22 15:09

Ned Batchelder