PHP动态生成javascript文件的2个例子
时间:2024-05-11 09:25:44
一、PHP动态生成
第一步:在PHP文件中直接写入JS代码,并在头部声明这是一个JavaScript文件
<?php header('Content-Type: application/x-javascript; charset=UTF-8');?>
第二步:用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为例
<?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('\\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("<?php echo jsformat("<a href=\"$v[url]\"><img src=\"$v[thumb]\"></a>");?>");
{/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的脚本文件。
页面上这样引用:
<script src="http://tg.1155t.cn/code/53/" language="JavaScript"></script>
标签:PHP,动态生成,javascript
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP二维码的生成与识别案例
2023-06-14 06:48:55
![](https://img.aspxhome.com/file/2023/2/55562_0s.png)
golang 输出重定向:fmt Log,子进程Log,第三方库logrus的详解
2024-04-27 15:40:14
python3+PyQt5+Qt Designer实现扩展对话框
2023-08-01 00:16:45
![](https://img.aspxhome.com/file/2023/6/91836_0s.png)
ASP XMLDom在服务器端操作XML文件的主要方法和实现
2011-03-09 11:09:00
1秒50万字!js实现关键词匹配
2024-05-02 16:18:04
![](https://img.aspxhome.com/file/2023/6/132706_0s.jpg)
php实现在服务器端调整图片大小的方法
2024-05-13 09:25:46
Django中select_related和prefetch_related的用法与区别详解
2023-10-08 12:38:08
![](https://img.aspxhome.com/file/2023/9/99409_0s.jpg)
语义化你的HTML标签和属性
2008-06-12 13:18:00
如何远程连接SQL Server数据库
2009-06-08 12:41:00
![](https://img.aspxhome.com/file/UploadPic/20096/8/mssql01-56s.jpg)
一文带你掌握Python中文词频统计
2022-11-17 21:02:00
![](https://img.aspxhome.com/file/2023/0/66880_0s.png)
Python实现单例模式的四种方式详解
2023-07-12 03:07:43
![](https://img.aspxhome.com/file/2023/5/97345_0s.png)
利用python批量修改word文件名的方法示例
2023-03-15 00:05:12
![](https://img.aspxhome.com/file/2023/0/127830_0s.png)
python爬虫框架scrapy实战之爬取京东商城进阶篇
2023-02-27 21:20:51
python标准算法实现数组全排列的方法
2022-03-18 15:11:38
讲解Python中运算符使用时的优先级
2022-12-28 14:40:17
![](https://img.aspxhome.com/file/2023/2/92382_0s.jpg)
Python脚本传参数argparse模块的使用
2023-02-28 23:07:21
![](https://img.aspxhome.com/file/2023/4/97724_0s.png)
Python查询IP地址归属完整代码
2022-10-18 16:39:07
MySQL如何对数据进行排序图文详解
2024-01-16 09:55:47
![](https://img.aspxhome.com/file/2023/9/83379_0s.png)
用户"sa"登陆失败 SQLServer 错误18456的解决方法
2024-01-18 18:04:37
js拖拽效果的原理及实现
2024-04-10 10:43:29