Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Basic solution */
- $js = array();
- $js[] = "/path/to/js/file.js";
- foreach( $js as $value )
- {
- printf( "<script type='text/javascript' src='%s'></script>\n", $value );
- }
- /* More complicated solution */
- abstract class PageJS
- {
- protected static $js = array();
- static public function add( $js, $type = "file", $group = "global" )
- {
- if( in_array( $type, array("file", "code") ) === false )
- {
- return false;
- }
- self::$js[$group][] = array( $js, $type );
- return true;
- }
- static public function display( $group = "global" )
- {
- if( empty( self::$js[$group] ) === true ) return false;
- foreach( self::$js[$group] as $value )
- {
- if( $value[1] == 'file' )
- {
- printf( "<script type='text/javascript' src='%s'></script>\n", $value[0] );
- }
- else
- {
- printf( "<script type='text/javascript>%s</script>\n", $value[0] );
- }
- }
- }
- }
- // Examples:
- // Adding Global JS
- PageJS::add( "/path/to/js/file.js" );
- PageJS::add( "/path/to/js/file.js", "file" ); // Same thing as example above
- PageJS::add( "
- $(function() {
- alert('Document ready!');
- });
- ", "code" );
- // Displaying Global JS
- PageJS::display();
- // Adding JS for specific groups
- PageJS::add( "/path/to/js/file.js", "file", "groupForCustomPage" );
- PageJS::add( "
- $(function() {
- alert('Document ready!');
- });
- ", "code", "groupForCustomPage" );
- // Displaying a custom group JS
- PageJS::display(); // display the global JS
- PageJS::display( "groupForCustomPage" ); // display the JS for specific page.
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement