zoukankan      html  css  js  c++  java
  • 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() {
    ");
    	while (($buffer = fgets($handle, 4096)) !== false) {
    		$s = addslashes($buffer);
    		$line = "	document.write("" . rtrim($s, "
    ") . "");
    ";
    		fwrite($dest, $line);
    	}
    	fwrite($dest, "}).call();");
    	if (!feof($handle)) {
    		echo "Error: unexpected fgets() fail
    ";
    	}
    	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();
    

      

  • 相关阅读:
    ajax专题
    luogu P1346 电车 最短路
    luogu P1462 通往奥格瑞玛的道路 最短路
    luogu P1328 生活大爆炸版石头剪刀布
    luogu P1315 联合权值 枚举
    luogu P1156 垃圾陷阱 背包问题
    luogu P1217 回文质数 枚举
    luogu P3650 滑雪课程设计 枚举
    luogu1209 修理牛棚 贪心
    luogu P1223 排队接水 贪心
  • 原文地址:https://www.cnblogs.com/mingzhanghui/p/9168384.html
Copyright © 2011-2022 走看看