black flat screen computer monitor
,

Use PHP to Create Dynamic JavaScript Values

One possible approach to writing JavaScript on-the-fly

I’ve been using something similar to the following in some apps for the purpose of including a sort of Dynamic JavaScript which, depending upon perhaps other variables or logical procedure, might easily be included or left out of any part of the web application.

$js_array=array();
$js_array['open']="<script type=\"text/javascript\"> \n";

$js_array['hideManForm']="function hideManForm(obj) { \n".
"var el = document.getElementById(obj); \n".
" if ( el.style.display != \"block\" ) { \n".
" el.style.display = \"block\"; \n".
" } \n".
" else { \n".
" el.style.display = \"none\"; \n".
" } \n".
"} \n";

$js_array['close']="</script> \n";

/*
SNIP
--------
*/
<html>
<!--
... SNIP
THEN SOMETHING LIKE THIS TO GET IT INTO THE DOCUMENT HEAD...
-->
<?php
foreach($js_array as $jsKey => $jsVal){
print $jsVal;
}
?>
</head>

I’ve found that this technique will also successfully create a Dynamic CSS block where needed as well.

Whatchu do


Leave a Reply

Your email address will not be published. Required fields are marked *