Is it possible to add a php code in a . js file?

Asked

Viewed 44 times

0

Well my question is if it is possible to add a code

If not, how can I make it possible.

Thank you

  • Well, in case it’s the other way around I want to write php, in a javascript code.

  • If the FILE is . php, you can include a php code within the js code... but if the file is js... no.

  • So, there’s no way? I have to pass js to the right.php file?

  • right... I think it’s the only way

1 answer

2


Rename the . js file to . php, after that, at the beginning of the file, add the following.

<?php
    header("Content-type: text/javascript");
?>

Now you can include PHP code both within these tags we created, as in any other part of the file, remembering to follow the basic rules of PHP.

This file will go through the PHP interpreter before it is sent to the client (as with a .php file with HTML, for example), and when it gets to the client, it will be a file. js common.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.