引用js文件时src=可传递参数,实现在一个js中引用另一个js文件

来源:互联网 发布:cool edit pro mac 编辑:程序博客网 时间:2024/06/13 19:40

html : 引用src参数value=2 

[html] view plain copy
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2. <head>  
  3.     <title></title>  
  4.     <script src="js/a.js?value=2" type="text/javascript"></script>  
  5.     <script type="text/javascript" language="javascript">  
  6.         a();  
  7.     </script>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div>  
  12.       
  13.     </div>  
  14.     </form>  
  15. </body>  
  16. </html>  

js/a.js: 读出传入参数value=2 ,引用b.js
[html] view plain copy
  1. var js = document.getElementsByTagName("script");  
  2. for (var i = 0; i < js.length; i++) {  
  3.     if (js[i].src.indexOf("a.js") >= 0) {  
  4.         var arraytemp = new Array();  
  5.         arraytemp = js[i].src.split('?');  
  6.         arraytemp = arraytemp[1].split('=');  
  7.         alert(arraytemp[0] + "=" + arraytemp[1]);  
  8.     }  
  9. }  
  10.   
  11. document.write("<script language='javascript' src='js/b.js'></script>");  
  12.   
  13. function a() {  
  14.     b();  
  15. alert('This is aa!');  
  16.   
  17. }  
js/b.js:
[html] view plain copy
  1. function b() {  
  2.     alert('This is bb!');  

1 0
原创粉丝点击