on a website project i want to use some javaScript only on some pages not on whole project. if i include js into header.php then it will render every time on every page which i don't want. what is the best and standard method to do this?
switch ($pageUrl) { case \"page_name_1\": case \"page_name_2\": case \"page_name_3\" : echo '<script type=\"text/javascript\" language=\"javascript\" src=\"scripts/file.js\"></script>'; break; }
What this script dose is that it checks the page name of the currently requested file and if it matches the one you want it outputs the JavaScript code.
A firebug extension YSLOW suggest to put javascript in bottom of the page. but it is semantic or correct . as i know javascript should be placed in <head> only.
i'm using this format
Header.php
+
content.php = index.php
+
footer.php
So you'll have:
header.php
javascript.php
content.php and so on...
and only include the JavaScript when you need it to be rendered
What this script dose is that it checks the page name of the currently requested file and if it matches the one you want it outputs the JavaScript code.