思路话语

。Arlen:思想有多远你就能走多远...

JS文件中调用JS文件提示找不到对象的解决办法

今天网友提出这样一个问题:
<script type="text/javascript">
var arrBaiduCproConfig=new Array();
arrBaiduCproConfig['uid'] =388590;
arrBaiduCproConfig['n'] ='stockcity_cpr';
arrBaiduCproConfig['tm'] ='40';
arrBaiduCproConfig['cm'] ='100';
arrBaiduCproConfig['um'] ='40';
arrBaiduCproConfig['w'] ='255';
arrBaiduCproConfig['h'] ='54';
arrBaiduCproConfig['wn'] ='1';
arrBaiduCproConfig['hn'] ='1';
arrBaiduCproConfig['ta'] ='center';
arrBaiduCproConfig['tl'] ='bottom';
arrBaiduCproConfig['bu'] ='1';
arrBaiduCproConfig['bd'] ='#trans';
arrBaiduCproConfig['bg'] ='#trans';
arrBaiduCproConfig['tt'] ='#0000ff';
arrBaiduCproConfig['ct'] ='#444444';
arrBaiduCproConfig['url'] ='#008000';
arrBaiduCproConfig['bdl'] ='#000000';
arrBaiduCproConfig['rad'] =1;
</script>
<script type="text/javascript" charset="gbk" src="http://cpro.baidu.com/cpro/ui/ui.js" >
</script>
<script type="text/javascript">
      <!--
document.write(baiduCproIFrame());
-->
</script>

百度的这段广告代码不能放在静态页面里,要求你用JS调用这段代码

我一开始这样来做:
建一个JS文件叫JScript1.js。在里头将百度的这段JS输出:
var str = "";
str 
+= "<script type=\"text/javascript\"> \n";
str 
+= "var arrBaiduCproConfig=new Array(); \n";
str 
+= "arrBaiduCproConfig['uid'] =388590;\n";
str 
+= "arrBaiduCproConfig['n'] ='stockcity_cpr';\n";
str 
+= "arrBaiduCproConfig['tm'] ='40';\n";
str 
+= "arrBaiduCproConfig['cm'] ='100';\n";
str 
+= "arrBaiduCproConfig['um'] ='40';\n";
str 
+= "arrBaiduCproConfig['w'] ='255';\n";
str 
+= "arrBaiduCproConfig['h'] ='54';\n";
str 
+= "arrBaiduCproConfig['wn'] ='1';\n";
str 
+= "arrBaiduCproConfig['hn'] ='1';\n";
str 
+= "arrBaiduCproConfig['ta'] ='center'; \n";
str 
+= "arrBaiduCproConfig['tl'] ='bottom';\n";
str 
+= "arrBaiduCproConfig['bu'] ='1';\n";
str 
+= "arrBaiduCproConfig['bd'] ='#trans';\n";
str 
+= "arrBaiduCproConfig['bg'] ='#trans';\n";
str 
+= "arrBaiduCproConfig['tt'] ='#0000ff';\n";
str 
+= "arrBaiduCproConfig['ct'] ='#444444';\n";
str 
+= "arrBaiduCproConfig['url'] ='#008000';\n";
str 
+= "arrBaiduCproConfig['bdl'] ='#000000';\n";
str 
+= "arrBaiduCproConfig['rad'] =1;\n";
str 
+= "</script> \n";
str 
+= "<script type=\"text/javascript\" charset=\"gbk\" src=\"http://cpro.baidu.com/cpro/ui/ui.js\" > \n";
str += "</script> \n";
str 
+= "<script type=\"text/javascript\"> \n";
//str += "      <!-- \n";
str += "document.write(baiduCproIFrame()); \n";
//str += "--> \n";
str += "</script>\n";
document.write(str);




然后在主页中这样调用:
<HTML>
    
<HEAD>
        
<title>JsCallJs</title>    
                
<script language="javascript" src="JScript1.js"></script>    
        
    
</HEAD>
    
<body>
        
<form id="Form1" method="post" runat="server">
        
        
</form>
    
</body>
</HTML>

看上去没有一点问题。但是结果是出错了:缺少对象!

想了半天,终于摸出门路来,这个错误似乎是这个意思:调用baiduCproIFrame()之前并没有真正把http://cpro.baidu.com/cpro/ui/ui.js这个文件调用进来。
于是手动把这句复制出来,加在主页的 JScript1.js句子之前(注意:不是剪出来,而是COPY。这样并不破坏原来的代码)。
问题解决!!!

主页最后的情况如下:
<HEAD>
            
<title>JsCallJs</title>      
            
<script type="text/javascript" charset="gbk" src="http://cpro.baidu.com/cpro/ui/ui.js"></script>      
            
<script language="javascript" src="JScript1.js"></script>      
            
      
</HEAD>

不知道各位还有没有更好的解决方法。希望能在此讨论。

posted on 2007-04-12 13:41  Arlen  阅读(6646)  评论(4编辑  收藏  举报

导航