[php] how to include js file in php?

I have a problem with an js file in php. if i include it like this:

?> <script type="text/javascript" href="file.js"></script> <?php

the file isn't included and i get an error that the function isn't defined.

When i try it this way:

<script type="text/javascript">
document.write('<script type="text/javascript" src="datetimepicker_css.js"></script>');
</script>

the first tag in the document.write function closes <script type="text/javascript">

what is the correct way to do this?

thanks, sebastian

This question is related to php javascript

The answer is


There is no difference between HTML output by PHP and a normal HTML page in this regard.

You should definitely not have to do the document.write. Your JS is not related to your PHP, so there must be some other error (like the file path) in the first example.

On a side note, you'll have problems using </script> within a script. The HTML parser sees the closing script tag when scanning the JS before parsing it, and becomes confused. You see people doing things like '<'+'/script' for this reason.

Edit:

As T.J. Crowder pointed out, actually you need to change 'href to src in the <script> tag. Oops, actually Pekka pointed that out in his answer itself, so actually adding it here is totally superfluous.


If you truly wish to use PHP, you could use

include "file.php";

or

require "file.php";

and then in file.php, use a heredoc & echo it in.

file.php contents:
$some_js_code <<<_code
function myFunction()
{
   Alert("Some JS code would go here.");
}
_code;

At the top of your PHP file, bring in the file using either include or require then in head (or body section) echo it in

<?php
require "file.php";
?>
<html>
<head>
<?php
echo $some_js_code;
?>
</script>
</head>
<body>
</body>
</html>

Different way but it works. Just my $.02...


I found a different solution that I like:

<script>
    <?php require_once("/path/to/file.js");?>
</script>

Also works with style-tags and .css-files in the same way.


In your example you use the href attribute to tell where the JavaScript file can be found. This should be the src attribute:

?> <script type="text/javascript" src="file.js"></script> <?php

For more information see w3schools.


I have never been a fan of closing blocks of PHP to output content to the browser, I prefer to have my output captured so if at some point within my logic I decide I want to change my output (after output has already been sent) I can just delete the current buffer.

But as Pekka said, the main reason you are having issues with your javascript inclusion is because your using href to specify the location of the js file where as you should be using src.

If you have a functions file with your functions inside then add something like:

function js_link($src)
{
    if(file_exists("my/html/root/" . $src))
    {
        //we know it will exists within the HTTP Context
        return sprintf("<script type=\"text/javascript\" src=\"%s\"></script>",$src);
    }
    return "<!-- Unable to load " . $src . "-->";
}

The n in your code without the need for closing your blocks with ?> you can just use:

echo js_link("jquery/1.6/main.js");

I tried this, I've got something like

script type="text/javascript" src="createDiv.php?id=" script

AND In createDiv.php I Have

document getElementbyid(imgslide).appendchild(imgslide5).innerHTML = 'php echo $helloworld; ';

And I got supermad because the php at the beginning of the createDiv.php I made the $helloWorld php variable was formatted cut and paste from the html page

But it wouldn't work cause Of whitespaces was anyone gonna tell anyone about the whitespace problem cause my real php whitespace still works but not this one.


Its more likely that the path to file.js from the page is what is wrong. as long as when you view the page, and view-source you see the tag, its working, now its time to debug whether or not your path is too relative, maybe you need a / in front of it.


Pekka has the correct answer (hence my making this answer a Community Wiki): Use src, not href, to specify the file.

Regarding:

When i try it this way:

<script type="text/javascript">
    document.write('<script type="text/javascript" src="datetimepicker_css.js"></script>');
</script>

the first tag in the document.write function closes
what is the correct way to do this?

You don't want or need document.write for this, but just in case you ever do need to put the characters </script> inside a script tag for some other reason: You do that by ensuring that the HTML parser (which doesn't understand JavaScript) doesn't see a literal </script>. There are a couple of ways of doing that. One way is to escape the / even though you don't need to:

<script type='text/javascript'>
alert("<\/script>"); // Works, HTML parser doesn't see this as a closing script tag
//      ^--- note the seemingly-unnecessary backslash
</script>

Or if you're feeling more paranoid:

<script type='text/javascript'>
alert("</scr" + "ipt>"); // Works, HTML parser doesn't see this as a closing script tag
</script>

...since in each case, JavaScript sees the string as </script> but the HTML parser doesn't.