Combining multiple files into a single HTTP request

on twitter davelaker asked:

Question

“David Laker @ijasonwhite I have JS files tht are called separately I want to make fewer HTTP requests without actually physically combining them .”

Answer

Document uses file “combined.js.php” as javascript source, this is the file that will be HTTP requested and also the file that does the combining.

Source code for “combined.js.php”

<?php
header('Content-type: text/javascript');
readfile('file1.js');
readfile('file2.js');
?>

 

as you can see it’s using and combining two .js files, the source code for each file is below:

file1.js”

// JavaScript Document
alert("Hello File 1");

 

“file2.js”

// JavaScript Document
alert("Hello File 2");

 

in your HTML header include the combined files by adding the follow script tags.

<script language="javascript" src="combined.js.php"></script>

That’s about it! click here to see it in action

No Responses to “Combining multiple files into a single HTTP request”

Post a Comment