This article mainly introduces PHP dynamic generation JavaScript File 2 examples, need friends can refer to the following
PHP dynamic Generation First step: Write the JS code directly in the PHP file, and declare it in the header this is a JavaScript file code as follows: <?php header (' Content-type: Application/x-javascript; Charset=utf-8 ');? > Step two: Use PHP to output escape JavaScript code code as follows: function Jsformat ($str) { $STR = Trim ($STR); $STR = s Tr_replace (' SS ', ' s ', $str); $STR = Str_replace (CHR), ", $STR); $STR = Str_replace (Chr, ", $STR); $STR = Str_replace (' ', ', $str); $STR = Str_replace (', ', $str); $STR = Str_replace (' "', '" ", $str); $STR = Str_replace (",", $str); $STR = Str_replace ("'", "'", $str); return $STR; Direct call to Jsformat ($STR) Last step: URL rewrite, such as PHP address for xxx/123.php as long as the rewrite xxx/123.js to achieve the goal. Take Phpcms as an example code as follows: <?php header (' Content-type:application/x-javascript; Charset=utf-8 ');? > {pc:content action= "position" posid= "1" order= "id DESC" num= "7" $catid =11} <?php function Jsformat ($str) { $STR = Trim ($STR); $STR = str_replace (' ss ', ' s ', $str); $STR = Str_replace (CHR), ", $STR); $STR = Str_replace (Chr, ", $STR); $STR = Str_replace (' ', ', $str); $STR = Str_replace (', ', $str); $STR = Str_replace (' "', '" ", $str); $STR = Str_replace (",", $str); $STR = Str_replace ("'", "'", $str); return $STR; ?> {loop $data $v} document.writeln ("<?php Echo Jsformat (" <a href= "$v [url]" ><img src= "$v [thumb]" > </a> ");? > "); {/loop} {/PC} Each loop uses Document.writeln () to write out the transferred code. II, PHP include JS file through HTML to write JavaScript to introduce a PHP link, the PHP is actually generated JS file: code is as follows: if (= = = $ad _type_id) { Couplet ob_start (); include Tmpl_path. ' Code/duilian.js '; header ("Content-type:application/x-javascript"); $code = Ob_get_clean (); echo $code; in PHP include JS file, JS inside the Var swf soVariable, var swf = ', where the OB cache is used, note the header ("Content-type:application/x-javascript") and let the browser know that this is a JavaScript script file. page Such references: code as follows: <script src= "http://tg.1155t.cn/code/53/" language= "JavaScript" ></script >