2 Examples of PHP dynamically generating JavaScript files

Source: Internet
Author: User

  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 >  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.