php 页面公共部分 转化为js document.write(); 并由匿名函数包裹

页面公共部分以javascript  document.write()方式加载
生成的js放到需要的位置
 
footer.js 放到body底部引入
...
<script src="js/footer.js"></script>
</body>
 
--------------------------------------
php.exe所在文件夹添加到path环境变量, 在命令行输入
php footer.html
--------------------------------------
<?php
$handle = fopen("footer.html", "r"); // 输入html文件
$dest = fopen("footer.js", "w"); // 输出js文件

if ($handle) {
	fwrite($dest, "(function() {\n");
	while (($buffer = fgets($handle, 4096)) !== false) {
		$s = addslashes($buffer);
		$line = "\tdocument.write(\"" . rtrim($s, "\r\n") . "\");\n";
		fwrite($dest, $line);
	}
	fwrite($dest, "}).call();");
	if (!feof($handle)) {
		echo "Error: unexpected fgets() fail\n";
	}
	fclose($handle);
	fclose($dest);
}
?>

  

 

输入的文件:

<div id="footer">
	<div class="container text-center">
		<div class="copyright">Copyright©2017~2020 
			<a href="http://www.tsinghuadtv.com/" target="_blank">凌讯软件</a> 技术支持
			<a href="http://www.miitbeian.gov.cn" target="_blank">京ICP备13010031号</a>
		</div>
	</div>
</div>

  

 
输出的文件:
(function() {
document.write("<div id=\"footer\">");
document.write(" <div class=\"container text-center\">");
document.write(" <div class=\"copyright\">Copyright©2017~2020 ");
document.write(" <a href=\"http://www.tsinghuadtv.com/\" target=\"_blank\">凌讯软件</a> 技术支持");
document.write(" <a href=\"http://www.miitbeian.gov.cn\" target=\"_blank\">京ICP备13010031号</a>");
document.write(" </div>");
document.write(" </div>");
document.write("</div>");
}).call();

  

posted @ 2018-06-11 18:01  zhanghui_ming  阅读(716)  评论(0编辑  收藏  举报