How can I include a javascript file using php echo ?
I am creating a html page using php ---
echo "<html><head><link rel='stylesheet' type='text/css' <script src="Calculation.js"> </script> href='style.css' </head><body>";
I have included script code in the above line and then in form tag ---
<form action='/workdonecalculation/index.php?_pagination_off=1' method='post' class='form' onSubmit='return workdone();'>
But my php code doesn't call my javascript file (Calculation.js)
-
You can't call a JS file. You can call a function defined in that file.Havelock– Havelock2012年10月05日 11:19:22 +00:00Commented Oct 5, 2012 at 11:19
-
Come on, there are myards of examples for this. You need to echo a reference to the javascript file inside the head section of your html page.arkascha– arkascha2012年10月05日 11:20:50 +00:00Commented Oct 5, 2012 at 11:20
-
echo '<script src="URL_TO_YOUR_SCRIPT"> </script>';Rupesh Patel– Rupesh Patel2012年10月05日 11:31:23 +00:00Commented Oct 5, 2012 at 11:31
6 Answers 6
echo "<html><head><link rel='stylesheet' type='text/css'/> <script src='Calculation.js'></script>"
First close link tag, and you are using double quotes.. for the src= use single, because you are already using double in the echo "". So src='Calculation.js'
Also you can do this the easy way use variables.
$a=<<<HERE
<script src="Calculation.js"> </script>
HERE;
echo $a;
Also you can close php and start html where ever you wish
<?php
if($i)
{
?>
<script src=""></script>
<?php
}
?>
Is that what you wanted???
1 Comment
You didn't close <link> tag and <script> tag misplaced inside <link> tag. Also you need to escape the double quotes in src="Calculation.js" or simple you enclose with single quotes.
Here is the correct code,
echo "<html><head><link rel='stylesheet' type='text/css' href='style.css' /><script src='Calculation.js'></script> </head><body>";
1 Comment
You'll need to complete link tag first.
Try this
echo "<html><head><link rel='stylesheet' type='text/css' href='style.css' /> <script src='Calculation.js'></script> </head><body>";
Comments
Seems the link tag is not closed properly and the double quotes in script tag is not escaped.
Try the below code :
echo "<html><head><link rel='stylesheet' type='text/css' href='style.css'><script src='Calculation.js'></script></head></html>";
Comments
use single qoute:
echo '<html><head><link rel="stylesheet" href="style.css" type="text/css" /> <script type="text/javascript" src="script.js"></script> </head><body>';
hope it helps
Comments
print("<script type=\"text/javascript\" src=\"Calculation.js\"></script>");
Are you running this code on Windows, if not are you sure the file is called "Calculation.js" and not "calculation.js"? Also, is it definitely in the same directly as the PHP script that's calling it?