<script>tester();</script>
复制代码
test6.php
header('Content-Type: application/x-javascript; charset=UTF-8'); $str = $_GET["str"]; ?>
// javascript document
// by bbs.it-home.org
alert('');
function tester(string)
{
string ? alert(string) : alert('you call a function named tester');
}
?>
复制代码
第二种方法:
test.php
<script> </li>
<li> function loadjs(url,callback){ </li>
<li> var head = document.getElementsByTagName("head")[0]; </li>
<li> var script = document.createElement('script'); </li>
<li> script.onload = script.onreadystatechange = script.onerror = function (){ </li>
<li> if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return; </li>
<li> script.onload = script.onreadystatechange = script.onerror = null; </li>
<li> script.src = ''; </li>
<li> script.parentNode.removeChild(script); </li>
<li> script = null; </li>
<li> callback(); </li>
<li> } </li>
<li> script.charset = "gb2312"; </li>
<li> script.src = url; </li>
<li> try { </li>
<li> head.appendChild(script); </li>
<li> } catch (exp) {} </li>
<li> } </li>
<li> </li>
<li> function loadmultijs(url,callback){ </li>
<li> if(Object.prototype.toString.call(url)==='[object Array]'){ //是否数组 </li>
<li> this.suc = 0; //加载计数 </li>
<li> this.len = url.length; //数组长度 </li>
<li> var a = this; </li>
<li> for(var i = 0;i < url.length;i++){ </li>
<li> loadjs(url[i],function(){ a.suc++; if(a.suc == a.len) try{callback();}catch(e){} }); </li>
<li> } </li>
<li> } </li>
<li> else if(typeof(url) == 'string') loadjs(url,callback); </li>
<li> } </li>
<li> </li>
<li> loadjs("test5.php?return=value",function(){ alert(value); tester(value); }); </li>
<li> </script>
复制代码
test5.php
var value="this is value.";
复制代码
加载多JavaScript文件的实例:
var url = [
'ajax.php?ajax=1',
'functions.js'
];
loadmultijs(url,function(){ alert("加载完毕。"); /* 这里可以调用动态加载的JS文件的数据或方法 */ });
复制代码
2. 动态加载css文件
test.php
this document has a #e4e4e4 background, a 300px/400px div, and a arial/24px/red words.
复制代码
div.php
// declare the output of the file as CSS header('Content-type: text/css'); // include the script //include('others.php'); $width = $_GET['w']; $height = $_GET['h']; ?>
复制代码
div{width:=$width?>px;height:=$height?>px;border:blue 1px solid;}
复制代码
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?>}
Copy code
That’s it. The method of dynamically loading js and css in php has been introduced. It is recommended that you test it yourself. See if there are any problems with the specific implementation.