html代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>a</title>
</head><body>
<script language="javascript" src="1.js"></script>
</body>
</html>
1.js
var c;
document.write("<script src=\"2.js\" language=\"javascript\"><\/script>");
document.write(c);
2.js
c = "aaa";
显示的结果却是:undefined
该怎么写啊?
搜索他们讲的方法基本上都试过了
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>a</title>
</head><body>
<script language="javascript" src="1.js"></script>
</body>
</html>
1.js
var c;
document.write("<script src=\"2.js\" language=\"javascript\"><\/script>");
document.write(c);
2.js
c = "aaa";
显示的结果却是:undefined
该怎么写啊?
搜索他们讲的方法基本上都试过了
2.js
var my_a_id = "aaa";
1.s
document.write("<script src=\"2.js\" language=\"javascript\"><\/script>");
document.write(my_a_id);结果什么都没显示
document.write(my_a_id);这句执行的时候,2.js还没加载完毕,你可以在window.onload或者自己加个回调处理
function include(src,encoding,fun)
{
var s = document.createElement('script');
s.type='text/javascript';
s.charset=encoding; //'gb2312';
s.src = src;
var tags =document.getElementsByTagName('head');
if(typeof(fun)=='function'){
if( document.all ){
s.onreadystatechange = function(){
if(/(complete|loaded)/.test(this.readyState)){
fun(); s.onreadystatechange = null; s.parentNode.removeChild(s);
}};
}else{
s.onload = function(){ fun(); s.onload = null; s.parentNode.removeChild(s); };
}
}
tags[0].appendChild(s);
};
var c;
document.write("<script src=\"2.js\" language=\"javascript\"><\/script>");
</script><script>
document.write(c);
</script>
分开写
也可以参考这个