PHP动态生成javascript文件的2个例子_PHP
一、PHP动态生成
第一步:在PHP文件中直接写入JS代码,并在头部声明这是一个JavaScript文件
复制代码 代码如下:
第二步:用PHP输出转义JavaScript代码
复制代码 代码如下:function jsformat($str)
{
$str = trim($str);
$str = str_replace('\\s\\s', '\\s', $str);
$str = str_replace(chr(10), '', $str);
$str = str_replace(chr(13), '', $str);
$str = str_replace(' ', '', $str);
$str = str_replace('\\', '\\\\', $str);
$str = str_replace('"', '\\"', $str);
$str = str_replace('\\\'', '\\\\\'', $str);
$str = str_replace("'", "\'", $str);
return $str;
}
直接调用jsformat($str)
最后一步:进行url重写,比如PHP地址为 xxx/123.php 只要重写成 xxx/123.js 至此已经达成目的。
以PHPCMS为例
复制代码 代码如下:
{pc:content action="position" posid="1" order="id DESC" num="7" $catid=11}
function jsformat($str){
$str = trim($str);
$str = str_replace('\\s\\s', '\\s', $str);
$str = str_replace(chr(10), '', $str);
$str = str_replace(chr(13), '', $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("");?>");
{/loop}
{/pc}
每一个循环都用 document.writeln()写出转移后的代码。
二、PHP include JS文件
通过html写javascript引入一个php的链接,该php实际上是生成js的文件:
复制代码 代码如下:
if (20 == $ad_type_id) { // 对联
ob_start ();
include TMPL_PATH . 'Code/duilian.js';
header("content-type: application/x-javascript");
$code = ob_get_clean ();
echo $code;
}
在php里面include js文件,js里面的var swf这样的变量,var swf = ‘‘,这里使用ob缓存,注意加上header(“content-type: application/x-javascript”),让浏览器知道这是个javascript的脚本文件。
页面上这样引用:
复制代码 代码如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide
