Tutorials Home Scripts Home

Simple PHP JS loader

Simple PHP JS loader

The usual way, which most people know it


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

our way

<script type="text/javascript" src=js/loader/file|file1"</script>

why this method

  • to include js files easy in any time in your page
  • this way Prevent any intruder to find your js folder

How I Can Use!

just in your header between the <head></head> tags add this code
<script type="text/javascript" src="js/loader/file1|file2"></script>
Separate files by '|'

the files

  • loader.php
  • example.php
  • .htaccess
  • js/file.js
  • js/jquery.js //this is just jquery file you can download from any ware
loader.php
<?php

/**
 * @author Jooria Refresh Your Website
 * @file loader.php
 */

switch ($_GET['t']){ 
	case 'loader':
	//the folder that contan the js files change it please
	$dir = 'js';
	
	//to show the header as text/javascript
	header("Content-type:text/javascript");
	
	foreach( explode("|",$_GET['js']) as $key => $value ){
		
	$file = $dir."/$value.js";
	
	if (file_exists($file)) {
   //get the js files contents and read it only by r
    $handle = fopen($file, "r");
    
	print "\n//$value js (NO:$key)\n";
	//print the js file contents
    print fread($handle, filesize($file));
    
    //close the file
    fclose($handle);    

	}
		
	}
	
	break;

}


?>
.htaccess
RewriteEngine on
#SWITCH mod_rewrite ON
Options +FollowSymLinks
#OUR ABOVE CODE

RewriteRule ^js/(.*)/(.*)/?$ loader.php?t=$1&js=$2

example.php
<?php

/**
 * @author Jooria Refresh Your Website
 * @file example.php
 */
echo '<html>';
echo '<head>';
echo '<title>Simple PHP JS loader </title>';
echo '<script type="text/javascript" src="js/loader/jquery|file"></script>';
echo '</head>';
echo '<body>';
echo '<p><a>Hello world!</a></p>';
echo '</body>';
echo '</html>';

?>
js/file.js
document.write("This is my first file!");

Category: Website Programming | Views: 1,572 | on: December 01, 2009 | by: jooria
Enjoyed the article? Subscribe and get the new Tutorials

Enter your email address:

Last 3 Responses

Comment form

  1. muftah About 3 months ago
    thank u for that
  2. Adrian About 2 months ago
    Although it's a neat bit of coding, I can't see the justification for using it to join Javascript files I'm afraid. It will simply increase the load on the server, increase the bandwidth used, slow down the page loading times, and offers no additional security.

    Web servers are very good at serving static files quickly, whilst web browsers are very good at working out which files they've downloaded before and simply getting them from their cache instead. By feeding the static files to the client programmatically, you are running additional code on the server, whilst ensuring that the client won't know whether the file it receives can be cached or not; the end result is that it will download a fresh copy each time.

    In addition, most clients can also download multiple files simultaneously. With separate SCRIPT tags, the client can download two or more javascript files at the same time; with just a single script tag, it must download all the javascript content through a single connection.
    [/p]As for preventing intruders finding your JS folder, why would this be an issue? They can find my Images folder, my Flash folder, my CSS folder, etc, and that's never caused me a problem. As long as folder browsing is disabled, there is no security issue here at all.[/p]
  3. jooria About 2 months ago
    @Adrian you is really professional and this all right but i made this code for the scripts that make the js code is min

Leave a Comment

Comment form