Javascript错误:需要对象

时间:2011-02-10 08:17:22

标签: javascript

我在aspx页面的按钮中调用了一个javascript函数,如

OnClientClick= "printText(document.getElementById('PrintPayslipPart').innerHTML)"

和功能是;

function printText(elem)
     { 
        PrintPaySlip = window.open('RP_PrintPaySlip.html','PrintPaySlip','toolbar=no,menubar=yes,width=1000, Height = 700, resizable=yes,scrollbar=Yes'); 
        PrintPaySlip.document.open(); 
        PrintPaySlip.document.write("<html><head>");
        PrintPaySlip.document.write("</head><body onload='print()'>"); 
        PrintPaySlip.document.write(elem); 
        PrintPaySlip.document.write("</body></html>"); 
        PrintPaySlip.document.close();
    } 

我正在使用.net 3.5ajaxcontrolltoolkit 3.5.40412.2

单击按钮时,错误显示为“Microsoft JScript运行时错误:需要对象”。

2 个答案:

答案 0 :(得分:1)

我的猜测是

  1. PrintPayslipPart不是有效的ID,因此getElementById返回null。
  2. PrintPaySlip不是全局变量,您的环境不允许它   隐式定义,可以通过使用var

    将其声明为本地来解决
      var PrintPaySlip = window.open(...);
    
  3. 第二个似乎更有可能。

    HTH

答案 1 :(得分:0)

我建议您首先安装 Firefox并安装错误控制台,然后测试该网站。至少它可以帮助您找到确切的错误而不是“Microsoft JScript运行时错误”

相信我,但Firefox + FireBug + Error Console让网络(JS)开发人员的生活更美好。