@@ -61,6 +61,7 @@ class HTMLDocument {
6161 private $ dynCssSheets = array ();
6262 private $ templates = array ();
6363 private $ javascripts = array ();
64+ private $ javascriptsBody = array ();
6465 private $ static = false ;
6566 /**
6667 * this picks up the If-None-Match from the header from the headers
@@ -99,7 +100,6 @@ public function __construct()
99100 $ this ->browserCachedEtag = trim ($ requestHeaders ['If-None-Match ' ]);
100101 }
101102 }
102-
103103 $ this ->document ['header ' ] = array ();
104104 $ this ->document ['head ' ]['baseUrl ' ] = '' ;
105105 $ this ->document ['head ' ]['title ' ] = '' ;
@@ -119,6 +119,7 @@ public function __construct()
119119 $ this ->document ['body ' ]['marginWidth ' ] = '' ;
120120 $ this ->document ['body ' ]['marginHeight ' ] = '' ;
121121 $ this ->document ['body ' ]['onLoad ' ] = '' ;
122+ $ this ->document ['body ' ]['javaScript ' ] = '' ;
122123 }
123124
124125 const IE_COMPATIBILITY_MODE_IE7 = 'IE=7 ' ;
@@ -316,7 +317,7 @@ public function addHead($HTML)
316317 }
317318
318319 /**
319- * add a javascript string to the head - if you want to link to an external Javascript file use @see Foomo\HTMLDocument::addJavascripts
320+ * add a javascript string to the head - if you want to link to an external Javascript file use @see \ Foomo\HTMLDocument::addJavascripts
320321 *
321322 * @see Foomo\HTMLDocument::addJavascripts()
322323 * @param string $javascript
@@ -332,54 +333,118 @@ public function addJavascript($javascript)
332333 /**
333334 * add links to external Javascript files
334335 *
336+ * @example <code>\Foomo\HTMLDocument::getInstance()->addJavascripts(array('/tm/js/script.js', 'anotherscript.js'));</code>
337+ * @see \Foomo\HTMLDocument::addJavascript()
338+ * @param array $jsLinks
339+ *
340+ * @return \Foomo\HTMLDocument
341+ */
342+ public function addJavascripts (array $ jsLinks )
343+ {
344+ return $ this ->addJSLinksTo ($ jsLinks , 'javascripts ' );
345+ }
346+ /**
347+ * @param string $jsLink
348+ * @param integer $priority int position where to add this script (0 will be the first script to load)
349+ * @deprecated
350+ * @return \Foomo\HTMLDocument
351+ */
352+ public function addJavascriptFileWithPriority ($ jsLink , $ priority )
353+ {
354+ trigger_error (__METHOD__ . ' is deprecated use addJavascriptLinkWithPriority instead ' , E_USER_DEPRECATED );
355+ return $ this ->addJavascriptLinkWithPriority ($ jsLink , $ priority );
356+ }
357+
358+ /**
359+ * @param string $jsLink
360+ * @param integer $priority int position where to add this script (0 will be the first script to load)
361+ * @return \Foomo\HTMLDocument
362+ */
363+ public function addJavascriptLinkWithPriority ($ jsLink , $ priority )
364+ {
365+ return $ this ->addJSLinkWithPriorityTo ($ jsLink , $ priority , 'javascripts ' );
366+ }
367+
368+
369+ /**
370+ * add a javascript string to the body - if you want to link to an external Javascript file use @see Foomo\HTMLDocument::addJavascripts
371+ *
372+ * @see Foomo\HTMLDocument::addJavascripts()
373+ * @param string $javascript
374+ *
375+ * @return \Foomo\HTMLDocument
376+ */
377+ public function addJavascriptToBody ($ javascript )
378+ {
379+ $ this ->document ['body ' ]['javaScript ' ] .= $ javascript ;
380+ return $ this ;
381+ }
382+
383+ /**
384+ * add links to external Javascript files to the end of the body
385+ *
335386 * @example <code>$bert->HTMLDocument->addJavascripts(array('/tm/js/script.js', 'anotherscript.js'));</code>
336387 * @see Foomo\HTMLDocument::addJavascript()
337388 * @param array $jsLinks
338389 *
339390 * @return \Foomo\HTMLDocument
340391 */
341- public function addJavascripts ( $ jsLinks )
392+ public function addJavascriptsToBody ( array $ jsLinks )
342393 {
343- foreach ($ jsLinks as $ jsLink ) {
344- if (!in_array ($ jsLink , $ this ->javascripts )) {
345- array_push ($ this ->javascripts , $ jsLink );
346- }
347- }
348- return $ this ;
394+ return $ this ->addJSLinksTo ($ jsLinks , 'javascriptsBody ' );
349395 }
350396
351397 /**
352- * @param $jsLink
353- * @param $position int position where to add this script (0 will be the first script to load)
398+ * @param string $jsLink
399+ * @param integer $priority int position where to add this script (0 will be the first script to load)
354400 * @return \Foomo\HTMLDocument
355401 */
356- public function addJavascriptFileWithPriority ($ jsLink , $ position )
402+ public function addJavascriptLinkWithPriorityToBody ($ jsLink , $ priority )
357403 {
358- $ temp = $ this ->javascripts ;
404+ return $ this ->addJSLinkWithPriorityTo ($ jsLink , $ priority , 'javascriptsBody ' );
405+ }
406+
407+
408+ /**
409+ * @param string $jsLink
410+ * @param integer $priority
411+ * @param string $targetProp
412+ * @return $this
413+ */
414+ private function addJSLinkWithPriorityTo ($ jsLink , $ priority , $ targetProp )
415+ {
416+ // the performance here is ok, since if you have more than 5 js links you are doing it wrong ;)
417+ $ temp = $ this ->{$ targetProp };
359418 if (($ pos = array_search ($ jsLink , $ this ->javascripts )) !== FALSE ) {
419+ // it is already in the target - we need to get rid of it, so that we can put it in the right place
360420 unset($ temp [$ pos ]);
361421 }
362-
363- if ( $ position >= count ( $ temp )) {
422+ if ( $ priority >= count ( $ temp )) {
423+ // no conflict with the existing items just add it to the end
364424 $ temp [] = $ jsLink ;
365- $ this ->javascripts = $ temp ;
425+ $ this ->{ $ targetProp } = $ temp ;
366426 } else {
427+ // we need to resort things
367428 $ new = array ();
368429 $ i = 0 ;
369430 foreach ($ temp as $ item ) {
370- if ($ i == $ position ) {
431+ if ($ i == $ priority ) {
371432 $ new [] = $ jsLink ;
372433 $ i ++;
373434 }
374435 $ new [] = $ item ;
375436 $ i ++;
376437 }
377- $ this ->javascripts = $ new ;
438+ $ this ->{ $ targetProp } = $ new ;
378439 }
379-
380440 return $ this ;
381441 }
382442
443+ private function addJSLinksTo (array $ jsLinks , $ targetProp )
444+ {
445+ $ this ->{$ targetProp } = array_unique (array_merge ($ this ->{$ targetProp }, $ jsLinks ));
446+ return $ this ;
447+ }
383448 /**
384449 * Set a meta refresh for the document - maybe for a Javascript free slideshow?
385450 *
@@ -470,10 +535,7 @@ public function output($suppressHeaders = false)
470535 foreach ($ this ->metaData as $ key => $ val ) {
471536 $ this ->document ['head ' ]['meta ' ] .= '<meta name=" ' . $ key . '" content=" ' . $ val . '" ' . $ tagSuffix . PHP_EOL ;
472537 }
473- $ this ->javascripts = array_unique ($ this ->javascripts );
474- foreach ($ this ->javascripts as $ jsLink ) {
475- $ this ->document ['head ' ]['meta ' ] .= '<script language="JavaScript" src=" ' . $ jsLink . '" type="text/javascript"></script> ' . PHP_EOL ;
476- }
538+ $ this ->addJSLinksToOutput ($ this ->javascripts , $ this ->document ['head ' ]['meta ' ]);
477539 if (!empty ($ this ->document ['head ' ]['baseUrl ' ])) {
478540 $ this ->document ['head ' ]['baseUrl ' ] = '<base href=" ' . $ this ->document ['head ' ]['baseUrl ' ] . '" ' . $ tagSuffix . PHP_EOL ;
479541 }
@@ -495,6 +557,7 @@ public function output($suppressHeaders = false)
495557 if (!empty ($ this ->iECompatibilityMode )) {
496558 $ output .= '<meta http-equiv="X-UA-Compatible" content=" ' . $ this ->iECompatibilityMode .'"/> ' . PHP_EOL ;
497559 }
560+ $ headerJS = '' ;
498561 foreach ($ this ->document as $ docPartName => $ docPartArray ) {
499562 switch ($ docPartName ) {
500563 case 'header ' :
@@ -513,12 +576,7 @@ public function output($suppressHeaders = false)
513576 }
514577 break ;
515578 case 'javaScript ' :
516- if (strlen ($ headPart ) > 0 ) {
517- $ output .= '<script language="JavaScript" type="text/javascript"> ' . PHP_EOL . '// <![CDATA[ <!-- ' . PHP_EOL .
518- $ headPart .
519- PHP_EOL . '// --> ]]> ' . PHP_EOL . '</script> ' . PHP_EOL ;
520- ;
521- }
579+ $ this ->addJSToOutput ($ headPart , $ output );
522580 break ;
523581 default :
524582 $ output .= $ headPart ;
@@ -532,13 +590,22 @@ public function output($suppressHeaders = false)
532590 case 'content ' :
533591 $ bodyContent = '> ' . PHP_EOL . $ bodyPart ;
534592 break ;
593+ case 'javaScript ' :
594+ $ bodyJS = $ bodyPart ;
595+ break ;
535596 default :
536597 if (strlen ($ bodyPart ) > 0 ) {
537598 $ output .= ' ' . strtolower ($ bodyPartName ) . '=" ' . $ bodyPart . '" ' ;
538599 }
539600 }
540601 }
541- $ output .= $ bodyContent . '</body> ' ;
602+ $ output .= $ bodyContent . PHP_EOL ;
603+ // last but not blocking least
604+ // js links
605+ $ this ->addJSLinksToOutput ($ this ->javascriptsBody , $ output );
606+ // js
607+ $ this ->addJSToOutput ($ bodyJS , $ output );
608+ $ output .= '</body> ' ;
542609 break ;
543610 case 'frameset ' :
544611 $ output .= '</head> ' . PHP_EOL ;
@@ -555,6 +622,22 @@ public function output($suppressHeaders = false)
555622 }
556623 return $ output ;
557624 }
625+ private function addJSLinksToOutput (array $ jsLinks , &$ output )
626+ {
627+ foreach ($ jsLinks as $ jsLink ) {
628+ $ output .= '<script language="JavaScript" src=" ' . htmlspecialchars ($ jsLink ) . '" type="text/javascript"></script> ' . PHP_EOL ;
629+ }
630+ }
631+ private function addJSToOutput ($ js , &$ output )
632+ {
633+ if (!empty ($ js )) {
634+ $ output .=
635+ '<script language="JavaScript" type="text/javascript"> ' . PHP_EOL . '// <![CDATA[ <!-- ' . PHP_EOL .
636+ $ js . PHP_EOL .
637+ '// --> ]]> ' . PHP_EOL . '</script> ' . PHP_EOL
638+ ;
639+ }
640+ }
558641 /**
559642 * send http headers
560643 *
0 commit comments