Heim >Backend-Entwicklung >PHP-Tutorial >php 动态加载JavaScript或css文件的方法

php 动态加载JavaScript或css文件的方法

WBOY
WBOYOriginal
2016-07-25 08:59:50979Durchsuche
  1. <script>tester();</script>
复制代码

test6.php

  1. header('Content-Type: application/x-javascript; charset=UTF-8');
  2. $str = $_GET["str"];
  3. ?>
  4. // javascript document
  5. // by bbs.it-home.org
  6. alert('');
  7. function tester(string)
  8. {
  9. string ? alert(string) : alert('you call a function named tester');
  10. }
  11. ?>
复制代码

第二种方法: test.php

  1. <script> <li> function loadjs(url,callback){ <li> var head = document.getElementsByTagName("head")[0]; <li> var script = document.createElement('script'); <li> script.onload = script.onreadystatechange = script.onerror = function (){ <li> if (script && script.readyState && /^(?!(?:loaded|complete)$)/.test(script.readyState)) return; <li> script.onload = script.onreadystatechange = script.onerror = null; <li> script.src = ''; <li> script.parentNode.removeChild(script); <li> script = null; <li> callback(); <li> } <li> script.charset = "gb2312"; <li> script.src = url; <li> try { <li> head.appendChild(script); <li> } catch (exp) {} <li> } <li> <li> function loadmultijs(url,callback){ <li> if(Object.prototype.toString.call(url)==='[object Array]'){ //是否数组 <li> this.suc = 0; //加载计数 <li> this.len = url.length; //数组长度 <li> var a = this; <li> for(var i = 0;i < url.length;i++){ <li> loadjs(url[i],function(){ a.suc++; if(a.suc == a.len) try{callback();}catch(e){} }); <li> } <li> } <li> else if(typeof(url) == 'string') loadjs(url,callback); <li> } <li> <li> loadjs("test5.php?return=value",function(){ alert(value); tester(value); }); <li> </script>
复制代码

test5.php

  1. var value="this is value.";
复制代码

加载多JavaScript文件的实例:

  1. var url = [
  2. 'ajax.php?ajax=1',
  3. 'functions.js'
  4. ];
  5. loadmultijs(url,function(){ alert("加载完毕。"); /* 这里可以调用动态加载的JS文件的数据或方法 */ });
复制代码

2. 动态加载css文件 test.php

  1. this document has a #e4e4e4 background, a 300px/400px div, and a arial/24px/red words.
  • 复制代码

    div.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. $width = $_GET['w'];
    6. $height = $_GET['h'];
    7. ?>
    复制代码
    1. div{width:=$width?>px;height:=$height?>px;border:blue 1px solid;}
    复制代码

    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的方法就介绍完了,建议大家亲自动手测试下,看看具体的实现有没有问题。



    Stellungnahme:
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn