View Full Version : Inserting different javascript to each page when using header include

05-03-2009, 12:13 AM
I'm a newbie to PHP and programming.

Some javascripts need to be inserted in html head tag, but I'm currently inserting head part using require. e.g.
require ('header.php');

I saw the other day that inserting different title to different page using a simple PHP fuction.

function header ($title) {
<title> $title </title>

header (First page);
etc etc.

I think there must be a way like above to insert different javascripts to each page, because it's not efficient to insert the javascript which is not used on every page.

How do you smart guys insert some javascript on specific page using php functions?

Can anybody help me, please? Thanks in advance.

05-03-2009, 12:21 AM
You could let your header.php decide which javascript to include depending on which page was requested ($_SERVER["REQUEST_URI"]).

But it may be better if you put everything you may need into one javascript which you put into all the sites, because after the first time it's loaded it's stored in the browser cache anyway. Putting different javascripts into different pages means more HTTP requests and may slow down your site.