Free Exclusive And High Quality PSD Photoshop Resource For Web Design

Tricks

Best Way To Add Java Script Files In Any Page
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!");
in: » Website Programming » Tricks | Posted on Dec 1st, 2009 | views 7,698
About the author
i'm Muhammad from egypt i love doing one thing 'web programming & designing'