1

How to execute javascript+php in another file in current.js file?

The scenario as follows:

another-file.js

function foo()
{
    var a = <?php echo "Hello world !"; ?>
}

current.js

function show()
{
    var b = "Hi,";
    // execute javascript (with php code embeded)
    require(another-file.js)  // <-- this is not work

    alert(a + b); // I want result: "Hi, Hello World !"
}
  • 1
    What exactly are you trying to accomplish? What is your end goal? – cyberbit Jun 02 '16 at 03:53
  • JavaScript runs on the browser(client) whereas PHP on the server. – Sandeep Nayak Jun 02 '16 at 03:54
  • Currently I'm joining develop an old project, where there is a case that require us to solve a problem must follow above scenario. I know, it rather stranger (client vs server scripting that should be solved with Ajax or Jquery – user3246899 Jun 02 '16 at 03:59

2 Answers2

1

You can use PHP to generate the string that is assigned to a JS variable like so:

var myvar = "<?php echo "Hello World"; ?>";

In your example you forgot the quotes around the PHP tags (before <?php and after ?>) so you were probably getting errors because the JS interpreter was looking for varables called Hello and World instead of considering "Hello World" as a string.

Now as we know, you can't have includes in JS as you would in other languages, what people normally do is just using multiple <script> tags to include more than one JS into the page. However, if for some reason that I ignore, you absolutely need to include the JS into another JS, what you can do is using jquery's getScript(), see here for more info https://api.jquery.com/jquery.getscript/:

$.getScript( "another-file.js" )
    .done( function() {
        alert(a + b);
    })
    .fail( function() {
         console.log("Ooops there was a problem");
    });

EDIT: as I said in my other comment you can also send an AJAX query and then eval() (which is what getScript() does behind the scenes), or you can use ES6 modules, but that's beyond the scope of this question.

mastazi
  • 1,623
  • 26
  • 41
  • Thanks mastazi reminding me about the quote, but actually there is not my problem, it only a concept to explain my real poblem. – user3246899 Jun 02 '16 at 04:23
  • then I'm afraid I don't understand what's your "real problem" – mastazi Jun 02 '16 at 04:25
  • If you talk about the fact that `require` doesn't work in JS, then I'm going to edit the answer – mastazi Jun 02 '16 at 04:29
  • My real problem is how to load javascript code (where there is a sniped php code inside it) in x.js file, so it can be runned in z.js file. – user3246899 Jun 02 '16 at 04:29
  • Please be careful about the bold text in my answer: I thought it was obvious but I wanted to stress that the "normal" way of doing it is just using multiple script tags in your HTML. – mastazi Jun 02 '16 at 04:44
  • Thanks mastazi, It work when the included file is fully javascript code and error when there is php code embeded inside it. – user3246899 Jun 02 '16 at 04:54
  • Then it's a PHP problem; can you show the PHP code that returns the `another-file.js`? – mastazi Jun 02 '16 at 04:59
  • If `another-file.js` is not being returned by PHP code, than try just renaming it from `.js` to `.php` – mastazi Jun 02 '16 at 05:01
  • The PHP code is just a function that returned a data from a database. The good news, if I merge both file, it work. But when I devided them in separately file like my above scenario, it fail. Yes, I have tried it from .js to .php – user3246899 Jun 02 '16 at 05:13
  • I'm just curious when I loaded .js file the PHP code would not returned, when I loaded the .php file the javascript then would not executed. – user3246899 Jun 02 '16 at 05:14
  • PHP code by default is only executed if the file has extension .php so the above makes sense. I'm not sure why after renaming it wasn't executed, you could investigate by using the console in your browser's developer tools. – mastazi Jun 02 '16 at 05:15
  • Actually an easier way to test would be: rename to .php and then replace the `done` callback with the following `.done(function( script ) { console.log( 'script is: '+script ); })` – mastazi Jun 02 '16 at 05:18
  • Then let me know what you see in the console. – mastazi Jun 02 '16 at 05:18
  • Index.php 65:25 Ooops there was a problem – user3246899 Jun 02 '16 at 05:39
  • That error means you are using the wrong path. When you type `$.getScript( "another-file.php" )` you have to make sure that "another-file.php" exists. Obviously if you rename the file to `.php` you also have to rename it in the function call, I didn't specify it because I thought it was obvious – mastazi Jun 02 '16 at 05:42
  • Thank you mastazi, it work now. It turn out the problem is in my php code, I just realize that the first think is to make sure php should be returned successful in javascript. – user3246899 Jun 02 '16 at 08:37
-1

There's a couple things wrong here.

  1. You cannot execute PHP code in a JavaScript file. PHP is a server-side language, JavaScript is (traditionally) a client-side language. You can't execute PHP code with a JavaScript interpreter, or vice versa.
  2. You cannot include a JavaScript file from another JavaScript file. Rather, you must include both scripts on a webpage, like this:

    <body>
      <script src="file1.js"></script>
      <script src="file2.js"></script>
      <script>
        functionFromFile1();
        functionFromFile2();
      </script>
    </body>
    

I feel like there is a disconnect here that needs to be resolved.

cyberbit
  • 1,345
  • 15
  • 22
  • Yes, I know that. Actually this in an old project, and I see there is something case where php code embeded in javascript code, and it work. something like this snipe code: name: 'ITEM', data: [ , ], stack: 'inti' – user3246899 Jun 02 '16 at 04:04
  • You can actually include a JS file into another, even though it's not as straightforward as in other languages. You can use JQuery's `getScript()` or you can `eval()` a script which is returned by an AJAX request. In addition, ES6 provides modules that can be imported. – mastazi Jun 02 '16 at 04:16
  • 1
    Yes, as we can see in this topic: http://stackoverflow.com/questions/950087/include-a-javascript-file-in-another-javascript-file. But, it only execute javascript code – user3246899 Jun 02 '16 at 04:24
  • If you go the AJAX route (or `getScript` which uses AJAX) it will also execute PHP within the JS code – mastazi Jun 02 '16 at 23:18