1

if my hello.js is like this

function jst()
{
var i = 0 ;
i = <?php echo 35; ?>
alert( i );
}

what i really want in netbeans is to interpret that .js file through php interpreter without changing my extension of hello.js to hello.php or in other words i dont wan to change extension of my file from js . the reason behind this is Because netbean provide special support(i.e editing, coloring of text etc) for files with .js extension .

this is how i am including file in index.php

<script>
 <?php include 'hello.php'?>;
</script>

code is working fine but i want to use hello.js instead hello.php in netbeans like as shown in following snippet

<script src="hello.js"></script>

what should i do?? how professional websites handle this issue??

*.js http://s13.postimg.org/vl6vo4fif/image.png

*.php http://s21.postimg.org/t7tuk42l3/after.png every thing has converted to plain text after changing extension

5
  • Did you read my answer. PHP is server side JS is Client side. You can use `<script type="text/javascript" src="hello.js"></script>. Why not ? But php inside hello.js is now wrong , because it's handled on the client side , no server can parse the content of hello.js !!! Commented Sep 5, 2013 at 20:57
  • k thanks for replying Commented Sep 5, 2013 at 21:38
  • but the problem is how i should isolate these two things in two seperate php and js file. i uploaded the images of before and after changing file extensions Commented Sep 5, 2013 at 21:39
  • It seems you really did not read my answer :( . I can not see <script type="text/javascript"> above your dragger = function . Look at my updated answer at the top. Commented Sep 5, 2013 at 21:54
  • sorry for responding late. . . .and thanks for ur detailed reply. Commented Sep 10, 2013 at 17:20

3 Answers 3

2

You can only do as the most used way, via parameter

hello.js

function jst(alertMe)
{
alert( alertMe );
}

index.php

<html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>jsFileTest</title>
     <script type="text/javascript" src="hello.js"></script>
     <script type="text/javascript">
       var alertMe = <?php echo 35; ?> ;
     </script>
    </head>       
<body>

<button onclick="jst(alertMe)">Try it</button>
</body>
</html>        

Develop your js within your php file.
If everything works as expected then you can outsource everything as a separate file .js

But remember : php is parsed and interpreted on server side . So everything outside php tags is completely ignored. Therefore :

<script type="text/javascript" src="jsFile.js"></script>

is pure html and will be ignored. On server side php knows nothing about the existence of these .js file and it will not load and parse it. But this is required if you want php also interpret this file too.

If you want to include it with a php file, you can do it like

Put <script type="text/javascript"> at the beginning. code completion starts again.

jsFile.php

enter image description here

index2.php

<html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>jsFileTest</title>
     <?php
       include_once 'jsFile.php';
     ?>   
    </head>       
<body>
<?php
 echo "myID = ".$myId."<br>";
?>   
<button onclick="myFunction()">Try it</button>
</body>
</html>        

Running :

enter image description here

But now we come to the important part.

Look at the html output source :

<html>
    <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>jsFileTest</title>

<script type="text/javascript">
function myFunction()
{
 alert("Hi from jsFile.php");
}
</script>

    </head>       
<body>
myID = idontknow<br>   
<button onclick="myFunction()">Try it</button>
</body>
</html>        

As you can see , javascript ( function myFuntion() ) is inserted directly in the html output. That is exactly what does not happen with

<title>jsFileTest</title>
<script type="text/javascript" src="jsFile.js"></script>


You can not use src="jsFile.php"

<script type="text/javascript" src="jsFile.php"></script>

After the parsing is finished the content is sent to the client. From this moment it is useless to even try to parse embedded php code in javascript. (the server is no longer involved , has already done its work)

IE detects an error (Status Line). when you double-click this

enter image description here

Error window pops up

enter image description here

the browser expects valid javascript code and this

$myId = "idontknow";

is not a valid JS code.

Sign up to request clarification or add additional context in comments.

Comments

1

You just need to enable the PHP to read JS files. Do this:

Open your httpd.conf (Apache configuration file) and find this line:

AddHandler application/x-httpd-php .php

And add the extension, modifying this line to:

AddHandler application/x-httpd-php .php .js

You can even add CSS files too.

Comments

0

put this in customJs.php

<?php ob_start(); ?>
<script type="text/javascript">
<?php ob_end_clean(); ?>
    alert('aaaa');
<?php ob_start(); ?>
</script>
<?php ob_end_clean(); ?>

1 Comment

Welcome to SO. Could you explain what this does and how it solves the OPs problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.