Sådan benyttes komponenten Script klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Script.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Script::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Script($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Script klassen
Den fulde PHP kildekode for Script klassen
<?php/** * @package base * @see HTML_BASE_UTIL_PATH.'/Script.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASE_COMMON_PATH.'/Html.php');if (defined('HTML_UTIL_COMPONENT_PATH')) { require_once(HTML_UTIL_COMPONENT_PATH.'/Url.php');}/** * Generates an SCRIPT element * <code> * Usage: * $script = new Script($src, $js); * $script->add($element); * print $script->getHtml(); * Or * $script = new Script($src, $js); * print $script->getStart(); * print "some javascript"; * print $script->getEnd(); * Or * print $script->getStart(); * print $script->getOnload($function); * print $script->getEnd(); * Or * Script::display($src, $js); * Or * Script::start($src, $js); * DoSomeJavascript(); * Script::end(); * Or * Script::start($src, $js); * Script::onload($function); * Script::end(); * </code> * @package base */class Script extends Html { /** * @var String $src The url to the javascript source file */ protected $src = ''; /** * @var String $js The javascript if any */ protected $js = ''; /** * @var String $type The type */ protected $type = 'text/javascript'; /** * Constructor * @param String $src The url to the javascript source file * @param String $js The javascript if any */ function __construct($src='', $js='') { parent::__construct(); $domainname = ''; if (defined('HTML_UTIL_COMPONENT_PATH') && $src !== '') { $strHttp = 'http:/'.'/'; if (strpos($src, $strHttp) !== false) { // Skip } else {// $domainname = Url::subdomain(DOMAIN_NAME); // Strip off subdomain names } } $this->src = $domainname.$src; $this->js = $js; } /** * Returns the html for the Javascript window onunload event * <code> * Usage: * $script = new Script(); * print $script->getOnunload($function); * </code> * @param String $function The name of the function to add to the window.onunload * @param String $runFirst The function is run before other, if true * @return String the complete html */ function getOnunload($function='', $runFirst=false) { return $this->getOnload($function, $runFirst, false); } /** * Returns the html for the Javascript window onload event * <code> * Usage: * $script = new Script(); * print $script->getOnload($function, $runFirst, $unload); * </code> * @param String $function The name of the function to add to the window.onload * @param String $runFirst The function is run before other, if true * @param String $unload The onload code is used if true, onunloaf is used if false * @return String the complete html */ function getOnload($function='', $runFirst=false, $unload=true) { $html = ''; if ($function != '') { static $id = 0; $id++; $loadName = $unload ? 'onload' : 'onunload'; $oldName = $loadName.'Current'; $nl = ''; $tab = ''; if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $nl = "\r\n"; $tab = "\t"; } $functions = ''; if ($runFirst) { $functions .= $tab.$tab.$tab.$function."();$nl"; $functions .= $tab.$tab.$tab.$oldName."();$nl"; } else { $functions .= $tab.$tab.$tab.$oldName."();$nl"; $functions .= $tab.$tab.$tab.$function."();$nl"; } $html .= "function ".$loadName.$id."Body() {".$nl; $html .= $tab."var ".$oldName." = window.$loadName;".$nl; $html .= $tab."if (typeof ".$oldName." !== 'function') {".$nl; $html .= $tab.$tab."window.".$loadName." = $function;".$nl; $html .= $tab."} else {".$nl; $html .= $tab.$tab."window.".$loadName." = function() {".$nl; $html .= $functions; // to call $html .= $tab.$tab."}".$nl; $html .= $tab."}".$nl; $html .= "}".$nl; $html .= $loadName.$id."Body();".$nl; } else { if (defined('DEBUG_LEVEL_SHOW_INFO') && DEBUG_LEVEL & DEBUG_LEVEL_SHOW_INFO) { $html .= "<!-- bodyOnload is empty -->\r\n"; } } return $html; } /** * Returns the start html for the script control * @return String the complete html */ function getStart() { $html = ''; $html .= '<script'; $html .= $this->getAttribute('src'); $html .= $this->getAttribute('type'); $html .= ">\r\n"; $html .= $this->getJs(); return $html; } /** * Returns the start html for the script control * @return String the complete html */ function getJs() { $html = ''; if ($this->getSizeof() > 0 || $this->js != '') { $html .= "/"."/<![CDATA[\r\n"; $html .= $this->js; $html .= $this->getElements(); // as html $html .= "\r\n/"."/]]>\r\n"; } return $html; } /** * Returns the end html for the script control * @return String the complete html */ function getEnd() { return "</script>\r\n"; } /** * Returns the html for the script control * @return String the complete html */ function getHtml() { $html = $this->html; $html .= $this->getStart(); $html .= $this->getEnd(); return $html; } /** * Display the html for the window onload event * <code> * Usage: * Script::onload($function); * </code> * @static * @param String $function The name of the function to add to the window.onload * @param String $runFirst The function is run before other, if true */ public static function onload($function='', $runFirst=false) { $html = new Script(); $html->addHtml($html->getOnload($function, $runFirst)); } /** * Display the html for the window onunload event * <code> * Usage: * Script::onunload($function); * </code> * @static * @param String $function The name of the unload function to add to the window.onunload * @param String $runFirst The function is run before other, if true */ public static function onunload($function='', $runFirst=false) { $html = new Script(); $html->addHtml($html->getOnunload($function, $runFirst)); } /** * Start of tag * <code> * Usage: * Script::start($src, $js); * </code> * @static * @param String $src The url to the javascript source file * @param String $js The javascript if any */ public static function start($src='', $js='') { $html = new Script($src, $js); $html->addHtml($html->getStart()); } /** * End of tag * <code> * Usage: * Script::end(); * </code> * @static */ public static function end() { $html = new Script(); $html->addHtml($html->getEnd()); } /** * Display html * <code> * Usage: * Script::display($src, $js); * </code> * @static * @param String $src The url to the javascript source file * @param String $js The javascript if any */ public static function display($src='', $js='') { $html = new Script($src, $js); $html->addHtml(); }}?>
Den fulde HTML kildekode for Script klassen
<? <!-- DEBUG: Script --> <script src="/include/my.js" type="text/javascript"> </script> ?>
Her er 'klasse metoderne' for Script klassen:
Her er 'objekt variable' for Script klassen: