php 動態載入JavaScript或css檔案的方法

WBOY
發布: 2016-07-25 08:59:50
原創
930 人瀏覽過
  1. 測試器( );腳本>
複製程式碼

test6.php

  1. header('Content-Type: application/x-javascript; charset=UTF-8');
  2. $str = $_GET["str"];
  3. ? >
  4. // javascript 文件
  5. // by bbs.it-home.org
  6. alert('');
  7. 函數測試器(字串)
  8. {
  9. 字串? alert(string) :alert('你呼叫了一個名為tester 的函數');
  10. }
  11. ?>
複製程式碼

第二種方法: 測試.php

  1. function loadjs(url,callback){
  2. var head = document.getElementsByTagName("head")[0];
  3. var script = document.createElement('script');
  4. script.onload = script.onreadystatechange = script.onerror = function (){
  5. if (script && script.readyState && /^((State! (?:loaded|complete)$)/.test(script.就緒狀態))回傳;
  6. script.onload = script.onreadystatechange = script.onerror = null;
  7. script.src = '';
  8. script.parentNode.removeChild(script);
  9. 腳本= null;
  10. 回呼();
  11. }
  12. script.charset = "gb2312";
  13. script.src = url; > 試{
  14. head.appendChild(script);
  15. } catch (exp) {}
  16. }
  17. function loadmultijs(url,callback){
  18. if(Object.prototype. toString.call(url)==='[object Array] ]'){ // 是否有吞吐量
  19. this.suc = 0; //載入統計
  20. this.len = url.length; //吞吐量
  21. var a = this;
  22. for(var i = 0;i loadjs(url[i],function(){ a.suc++; if(a .suc == a.len) 試{callback();}catch(e){} });
  23. }
  24. }
  25. else if(typeof(url) == 'string') loadjs( url,callback);
  26. }
  27. loadjs("test5.php?return=value",function(){alert(value); tester(value); });
  28. 腳本>
複製程式碼
test5.php

  1. var value="這是值。";
複製代碼
加載多JavaScript檔案的實例:

  1. var url = [
  2. 'ajax.php?ajax=1',
  3. 'functions.js'
  4. ];
  5. ];
  6. loadmultijs(url,function(){alert("載入完成。"); /* 這裡可以呼叫動態載入的JS檔案的資料或方法*/ });
複製程式碼

2. 動態載入css文件 測試.php

  1. ;
  2. ;
  3. 文件有#e4e4e4 背景、300px/400px div 和arial/24px/red 文字。
  • 身體>
  • 複製程式碼

    div.php

    1. // 將檔案的輸出宣告為CSS
    2. header('Content-type: text/css') ;
    3. // 包含腳本
    4. //include('others.php');
    5. $width = $_GET['w'];
    6. $height = $_GET[' h'];
    7. ? >
    複製程式碼
    1. div{width:=$widthth>
    div{width:=$widthth>px; :=$height?>px;border:blue 1pxsolid;}
    複製程式碼

    fonts.php

    1. // declare the output of the file as CSS
    2. header('Content-type: text/css');
    3. // include the script
    4. //include('others.php');
    5. $size = $_GET['s'];
    6. $color = $_GET ['c'];
    7. ?>
    8. body{font-family:arial;font-size:=$size?>px;color:=$color?>}
    複製程式碼

    就是這些了,php動態載入js與css的方法就介紹完了,建議大家親自動手測試下,看看具體的實作有沒有問題。



    來源:php.cn
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!