test6.php
-
-
header('Content-Type: application/x-javascript; charset=UTF-8');
- $str = $_GET["str"];
- ? >
-
- // javascript 文件
- // by bbs.it-home.org
- alert('');
-
- 函數測試器(字串)
- {
- 字串? alert(string) :alert('你呼叫了一個名為tester 的函數');
- }
- ?>
複製程式碼
第二種方法:
測試.php
-
-
- function loadjs(url,callback){
- var head = document.getElementsByTagName("head")[0];
- var script = document.createElement('script');
- script.onload = script.onreadystatechange = script.onerror = function (){
- if (script && script.readyState && /^((State! (?:loaded|complete)$)/.test(script.就緒狀態))回傳;
- script.onload = script.onreadystatechange = script.onerror = null;
- script.src = '';
- script.parentNode.removeChild(script);
- 腳本= null;
- 回呼();
- }
- script.charset = "gb2312";
- script.src = url; > 試{
- head.appendChild(script);
- } catch (exp) {}
- }
-
- function loadmultijs(url,callback){
- if(Object.prototype. toString.call(url)==='[object Array] ]'){ // 是否有吞吐量
- this.suc = 0; //載入統計
- this.len = url.length; //吞吐量
- var a = this;
- for(var i = 0;i loadjs(url[i],function(){ a.suc++; if(a .suc == a.len) 試{callback();}catch(e){} });
- }
- }
- else if(typeof(url) == 'string') loadjs( url,callback);
- }
-
- loadjs("test5.php?return=value",function(){alert(value); tester(value); });
- 腳本>
-
複製程式碼
test5.php
加載多JavaScript檔案的實例:
- var url = [
- 'ajax.php?ajax=1',
- 'functions.js'
- ];
- ];
- loadmultijs(url,function(){alert("載入完成。"); /* 這裡可以呼叫動態載入的JS檔案的資料或方法*/ });
複製程式碼
2. 動態載入css文件
測試.php
-
-
- ;
-
-
-
-
; - 文件有#e4e4e4 背景、300px/400px div 和arial/24px/red 文字。
-
- 身體>
複製程式碼
div.php
-
-
// 將檔案的輸出宣告為CSS
- header('Content-type: text/css') ;
-
- // 包含腳本
- //include('others.php');
-
- $width = $_GET['w'];
- $height = $_GET[' h'];
- ? >
-
複製程式碼
div{width:=$widthth>px; :=$height?>px;border:blue 1pxsolid;}
複製程式碼fonts.php
-
-
// declare the output of the file as CSS
- header('Content-type: text/css');
-
- // include the script
- //include('others.php');
-
- $size = $_GET['s'];
- $color = $_GET ['c'];
- ?>
- body{font-family:arial;font-size:=$size?>px;color:=$color?>}
複製程式碼
就是這些了,php動態載入js與css的方法就介紹完了,建議大家親自動手測試下,看看具體的實作有沒有問題。
|