爱东东

VS.NET,IT,个人,个人网站 爱东东 http://www.idongdong.net

 

动态加载JS

www.123.com下的js文件a.js内容如下
var g_val1="123";
......
var g_valn="abc";

www.234.com下的a.aspx动态装载www.123.com/a.js并确认获取a.js定义的变量

<script language=javascript>
  function f_loadjs(scriptid,jsurl){
   var scriptTag = document.getElementById(scriptid);
   if(scriptTag) document.body.removeChild(scriptTag);
   script = document.createElement('script');
   script.setAttribute( "src" , jsurl);
   script.type = 'text/javascript';
   script.id = scriptid;
   document.body.appendChild(script)
  }
  function f_get_g_valn(){
   if(typeof(g_valn)=="undefined"){
    setTimeout("f_isgetdefined()",1000);
   }else{
    return g_valn;
   }
  }
  function f_calljs(){
   f_loadurl('calljs1','http://www.123.com/a.js");
   alert(f_get_g_valn());
  }
</script>
<input type=button onclick="f_calljs()" value="测试" />

posted on 2007-03-06 12:00  爱东东  阅读(701)  评论(0编辑  收藏  举报

导航