我把一个包含javascript脚本的html页面中的js代码提出来放到外部的js文件中,因为原代码比较长,我砍掉了大部分,只留下 核心的一些,在外部文件中我用到了类,但是运行的时候老说我的类没定义,找了很多资料我都没找出原因,请高手帮帮忙,看问题出在哪里,先谢谢大家了html原文件是这样的(经过简化的),运行时正常的:
<html>
<head>
<script type="text/javascript">
var STX1="a0";
var AddrP="00";
var TiltUpP="08";
var TiltDownP="10";
var ETX1="af";
var Synch="ff";
var AddrD="00";
var UpD="08";
var DownD="10";
var TiltPanSpeedMinM="22";
var TiltPanSpeedMedM="77";
var PROTOCOL_PELCO_P = 0;
var PROTOCOL_PELCO_D = 1;
var PTZProtocol;
var PTZ_SPEED;
var PTZTmp = "%d";//%d是由cgi传的值,下同
var BAUDRATE = "%d";
var PTZSpeed = "%d"
var SpeedDome_Type = "%d"
var address = %d;
var SpeedMinMikimi = "10";
var SpeedMedMikimi = "1f";
function sendCMD (CMDString)
{
  parent.frame2.Player.PtzControl(CMDString);//由框架2的ocx把命令传出去
}
function Pelco_P_Message(action1, action2, panspeed, titlespeed)
{
  var checksum= parseInt(STX1,16)^parseInt(AddrP,16)^parseInt(action1,16)^parseInt(action2,16)^parseInt(panspeed,16)^parseInt(titlespeed,16)^parseInt(ETX1,16);     
   
  if (checksum <= 0xf)
{
   checksum = "0" + checksum.toString(16);
  }
  return "0x" + STX1 + AddrP + action1 + action2 + panspeed + titlespeed + checksum.toString(16) + ETX1;
} function Pelco_D_Message(action1, action2, panspeed, titlespeed)
{
  var checksum;
  if(SpeedDome_Type == 4)  {
    checksum= (parseInt(AddrD,16)+parseInt(action1,16)+parseInt(action2,16)+parseInt(panspeed,16)+parseInt(titlespeed,16) %% parseInt("100",16));
  }
  else
  {
      checksum= parseInt(AddrD,16)+parseInt(action1,16)+parseInt(action2,16)+parseInt(panspeed,16)+parseInt(titlespeed,16);
  }
  
while (checksum > 0xff)
{
     checksum = checksum-0xff; 
  }
if (checksum <= 0xf)
{
   checksum = "0" + checksum.toString(16);
  }
  return "0x" + Synch + AddrD + action1 + action2 + panspeed + titlespeed + checksum.toString(16);
}function setAction(action)
{
  var PTZCmd; if (PTZProtocol == PROTOCOL_PELCO_P)
{
switch (action)
{
  case 0 : PTZCmd = Pelco_P_Message("00",TiltUpP,"00",PTZ_SPEED); 
    break;
  case 1 : PTZCmd = Pelco_P_Message("00",TiltDownP,"00",PTZ_SPEED);
    break;
}
  }
if (PTZProtocol == PROTOCOL_PELCO_D)
{
switch (action)
{
  case 0 : PTZCmd = Pelco_D_Message("00",UpD,"00",PTZ_SPEED); 
    break;
  case 1 : PTZCmd = Pelco_D_Message("00",DownD,"00",PTZ_SPEED); 
    break;
}
  }
  var CMDString = "ptzcmd&baud=" + BAUDRATE +"n81&cmd=" + PTZCmd;
  sendCMD(CMDString);
}
function onLoad(){
var fm  = document.control
ptzas.onload1(PTZTmp,PTZSpeed,address,SpeedDome_Type,BAUDRATE);
if (PTZTmp == 1){
     PTZProtocol = PROTOCOL_PELCO_D;
   }
    else if(PTZTmp == 0) {
     PTZProtocol = PROTOCOL_PELCO_P;
   } 
   if(PTZSpeed == 1)
     if(PTZProtocol == PROTOCOL_PELCO_D)
     {
        PTZ_SPEED = SpeedMinMikimi;
       
      }    
     else if(PTZProtocol == PROTOCOL_PELCO_P)
     {
        PTZ_SPEED = TiltPanSpeedMinM;
      } 
     
    }else if(PTZSpeed == 2){
      if(PTZProtocol == PROTOCOL_PELCO_D)
      {
       PTZ_SPEED = SpeedMedMikimi;
        
      }
     else if(PTZProtocol == PROTOCOL_PELCO_P)
     {
        PTZ_SPEED = TiltPanSpeedMedM;
      } 
       
    }
            
    }  if (PTZTmp == 0){
    if(address != 0)
      address = address -1;
    }
  
   if ( address <= 0xf){
   address = "0" + address.toString(16);
   }
   AddrP = address.toString(16);
   AddrD = address.toString(16);   }
</head><body class="menu_color" bgcolor="#cccccc" onselectstart="return false"  onpaste="return false" onLoad="onLoad()">
<td><input name="right" type="button"   onclick="setAction(0)">
  
</body>
</html>我修改后的文件:
html文件:<html>
<head>
<script type="text/javascript" src="ptz0.js"></script>
  <script language="JavaScript" type="text/javascript">
  var ptzas=new Ptz0();
  </script>
<script type="text/javascript">var PTZTmp = "%d";
var BAUDRATE = "%d";
var PTZSpeed = "%d"
var SpeedDome_Type = "%d"
var address = %d;
function onLoad()
{
   ptzas.onload1(PTZTmp,PTZSpeed,address,SpeedDome_Type,BAUDRATE);
}
</script></head><body class="menu_color" bgcolor="#cccccc" onselectstart="return false"  onpaste="return false" onLoad="onLoad()">
<td><input name="right" type="button"   onclick="ptzas.setAction(0)">
  
</body>
</html>

解决方案 »

  1.   

    帖子太长,下面继续
    js文件:ptz0.js
    function Ptz0()

      this.TiltUpP="08";  
      this.TiltDownP="10" ; 
      this.UpD="08";  
      this.DownD="10";  
      this.PTZProtocol;
      this.PTZ_SPEED;
      this.PROTOCOL_PELCO_P = 0;
      this.PROTOCOL_PELCO_D = 1;
      this.STX1="a0";
      this.AddrP="00";
      this.ETX1="af";
      this.AddrD="00";
      this.Synch="ff";
      this.TiltPanSpeedMinM="22";
      this.TiltPanSpeedMedM="77";
      this.SpeedMinMikimi = "10";
      this.SpeedMedMikimi = "1f";
      this.speedome_type;
      this.baudrate;
      this.PTZSpeed;
      this.Address;
      this.PTZtmp;
      this.onLoad1=function(ptztmp,ptzspeed,ADDress,speeddome_type,Baudrate)
      {         
        this.PTZtmp=ptztmp
        this.baudrate=Baudrate;
        this.speedome_type=speeddome_type;
        this.pan_dir=Pan_dir;
       if (this.PTZtmp == 1)
        {
         this.PTZProtocol = this.PROTOCOL_PELCO_D;
       }
        else if(this.PTZtmp == 0) 
        {
         this.PTZProtocol = this.PROTOCOL_PELCO_P;
       }
       this.PTZSpeed = ptzspeed;
      
         if(this.PTZSpeed == 1){
         if(this.PTZProtocol == this.PROTOCOL_PELCO_D)
         {
            this.PTZ_SPEED = this.SpeedMinMikimi;
          
          }    
         else if(this.PTZProtocol == this.PROTOCOL_PELCO_P)
         {
            this.PTZ_SPEED = this.TiltPanSpeedMinM;
          } 
        }else if(this.PTZSpeed == 2){
          if(this.PTZProtocol == this.PROTOCOL_PELCO_D)
          {
           this.PTZ_SPEED = this.SpeedMedMikimi;
          
          }
         else if(this.PTZProtocol == this.PROTOCOL_PELCO_P)
         {
            this.PTZ_SPEED = this.TiltPanSpeedMedM;
          }         
        }
      this.Address =ADDress;
       if (this.PTZtmp == 0){
        if(this.Address != 0)
          this.Address= this.Address -1;
        }
      
       if ( address <= 0xf){
       this.Address = "0" + this.Address.toString(16);
       }
       this.AddrP = this.Address.toString(16);
       this.AddrD = this.Address.toString(16);  
      }
      
     this.Pelco_D_Message=function (action1, action2, panspeed, titlespeed)
      {
      var checksum;
      if(this.speedome_type == 4) 
    {
        checksum= (parseInt(this.AddrD,16)+parseInt(action1,16)+parseInt(action2,16)+parseInt(panspeed,16)+parseInt(titlespeed,16) %% parseInt("100",16));
      }
      else
      {
          checksum= parseInt(this.AddrD,16)+parseInt(action1,16)+parseInt(action2,16)+parseInt(panspeed,16)+parseInt(titlespeed,16);
      }
      
    while (checksum > 0xff)
    {
         checksum = checksum-0xff; 
      }
    if (checksum <= 0xf)
    {
       checksum = "0" + checksum.toString(16);
      }
      return '0x'+ this.Synch + this.AddrD + action1 + action2 + panspeed + titlespeed + checksum.toString(16);
     }
    this.Pelco_P_Message=function (action1, action2, panspeed, titlespeed)
    {
      var checksum= parseInt(this.STX1,16)^parseInt(this.AddrP,16)^parseInt(action1,16)^parseInt(action2,16)^parseInt(panspeed,16)^parseInt(titlespeed,16)^parseInt(this.ETX1,16);     
       
      if (checksum <= 0xf)
    {
       checksum = "0" + checksum.toString(16);
      }
      return "0x" + this.STX1 + this.AddrP + action1 + action2 + panspeed + titlespeed + checksum.toString(16) + this.ETX1;
    }
    this.sendCMD=function (CMDString)
    {  parent.frame2.Player.PtzControl(CMDString);
    }
    this.setAction=function (action)
    {
      var PTZCmd;
    if (this.PTZProtocol == this.PROTOCOL_PELCO_P)
    {
    switch (action)
    {
      case 0 : this.PTZCmd = this.Pelco_P_Message("00",this.TiltUpP,"00",this.PTZ_SPEED); 
        break;
      case 1 : this.PTZCmd = this.Pelco_P_Message("00",this.TiltDownP,"00",this.PTZ_SPEED); 
        break; }

      }
    if (this.PTZProtocol == this.PROTOCOL_PELCO_D)
    {
    switch (action)
    {
      case 0 : this.PTZCmd = this.Pelco_D_Message("00",this.UpD,"00",this.PTZ_SPEED); 
        break;
      case 1 : this.PTZCmd = this.Pelco_D_Message("00",this.DownD,"00",this.PTZ_SPEED); 
        break;
    }
      }
      var this.CMDString = "ptzcmd&baud=" + this.baudrate +"n81&cmd=" + PTZCmd;
     this.sendCMD(CMDString);
    }
    }
      

  2.   

    采用必包可以解决,因为你放在外部的时候,没有自动调用,
    可以考虑把你的代码放在(function(){
    //dosomething
    })();
      

  3.   

    LZ 我是真的想看看你代码, 但是第一段代码调不通:
    ptza未定义, 还有%%符号 不能识别, 还有if逻辑错误, ect..最好你确定调通了, 在用HTML发上来吧.
      

  4.   

    代码中有错误,当然不能运行,以下已经修改过了,再试试function Ptz0(){
    this.TiltUpP = "08";
    this.TiltDownP = "10";
    this.UpD = "08";
    this.DownD = "10";
    this.PTZProtocol;
    this.PTZ_SPEED;
    this.PROTOCOL_PELCO_P = 0;
    this.PROTOCOL_PELCO_D = 1;
    this.STX1 = "a0";
    this.AddrP = "00";
    this.ETX1 = "af";
    this.AddrD = "00";
    this.Synch = "ff";
    this.TiltPanSpeedMinM = "22";
    this.TiltPanSpeedMedM = "77";
    this.SpeedMinMikimi = "10";
    this.SpeedMedMikimi = "1f";
    this.speedome_type;
    this.baudrate;
    this.PTZSpeed;
    this.Address;
    this.PTZtmp;
    this.onLoad1 = function(ptztmp, ptzspeed, ADDress, speeddome_type, Baudrate){
    this.PTZtmp = ptztmp
    this.baudrate = Baudrate;
    this.speedome_type = speeddome_type;
    this.pan_dir = Pan_dir;
    if (this.PTZtmp == 1) {
    this.PTZProtocol = this.PROTOCOL_PELCO_D;
    } else if (this.PTZtmp == 0) {
    this.PTZProtocol = this.PROTOCOL_PELCO_P;
    }
    this.PTZSpeed = ptzspeed;

    if (this.PTZSpeed == 1) {
    if (this.PTZProtocol == this.PROTOCOL_PELCO_D) {
    this.PTZ_SPEED = this.SpeedMinMikimi;

    } else if (this.PTZProtocol == this.PROTOCOL_PELCO_P) {
    this.PTZ_SPEED = this.TiltPanSpeedMinM;
    }
    } else if (this.PTZSpeed == 2) {
    if (this.PTZProtocol == this.PROTOCOL_PELCO_D) {
    this.PTZ_SPEED = this.SpeedMedMikimi;

    } else if (this.PTZProtocol == this.PROTOCOL_PELCO_P) {
    this.PTZ_SPEED = this.TiltPanSpeedMedM;
    }
    }
    this.Address = ADDress;
    if (this.PTZtmp == 0) {
    if (this.Address != 0) this.Address = this.Address - 1;
    }
    if (address <= 0xf) {
    this.Address = "0" + this.Address.toString(16);
    }
    this.AddrP = this.Address.toString(16);
    this.AddrD = this.Address.toString(16);
    }

    this.Pelco_D_Message = function(action1, action2, panspeed, titlespeed){
    var checksum;
    if (this.speedome_type == 4) {
    checksum = (parseInt(this.AddrD, 16) + parseInt(action1, 16) + parseInt(action2, 16) + parseInt(panspeed, 16) + parseInt(titlespeed, 16) % parseInt("100", 16));
    } else {
    checksum = parseInt(this.AddrD, 16) + parseInt(action1, 16) + parseInt(action2, 16) + parseInt(panspeed, 16) + parseInt(titlespeed, 16);
    }

    while (checksum > 0xff) {
    checksum = checksum - 0xff;
    }
    if (checksum <= 0xf) {
    checksum = "0" + checksum.toString(16);
    }
    return '0x' + this.Synch + this.AddrD + action1 + action2 + panspeed + titlespeed + checksum.toString(16);
    }
    this.Pelco_P_Message = function(action1, action2, panspeed, titlespeed){
    var checksum = parseInt(this.STX1, 16) ^ parseInt(this.AddrP, 16) ^ parseInt(action1, 16) ^ parseInt(action2, 16) ^ parseInt(panspeed, 16) ^ parseInt(titlespeed, 16) ^ parseInt(this.ETX1, 16);

    if (checksum <= 0xf) {
    checksum = "0" + checksum.toString(16);
    }
    return "0x" + this.STX1 + this.AddrP + action1 + action2 + panspeed + titlespeed + checksum.toString(16) + this.ETX1;
    }
    this.sendCMD = function(CMDString){

    parent.frame2.Player.PtzControl(CMDString);
    }
    this.setAction = function(action){
    var PTZCmd;
    if (this.PTZProtocol == this.PROTOCOL_PELCO_P) {
    switch (action) {
    case 0:
    this.PTZCmd = this.Pelco_P_Message("00", this.TiltUpP, "00", this.PTZ_SPEED);
    break;
    case 1:
    this.PTZCmd = this.Pelco_P_Message("00", this.TiltDownP, "00", this.PTZ_SPEED);
    break;
    }
    }
    if (this.PTZProtocol == this.PROTOCOL_PELCO_D) {
    switch (action) {
    case 0:
    this.PTZCmd = this.Pelco_D_Message("00", this.UpD, "00", this.PTZ_SPEED);
    break;
    case 1:
    this.PTZCmd = this.Pelco_D_Message("00", this.DownD, "00", this.PTZ_SPEED);
    break;
    }
    }
    this.CMDString = "ptzcmd&baud=" + this.baudrate + "n81&cmd=" + PTZCmd;
    this.sendCMD(CMDString);
    }
    }
      

  5.   


    <html>
    <head>
    <script type="text/javascript" src="ptz0.js">
    </script>
    <script language="JavaScript" type="text/javascript">
    var ptzas = new Ptz0();

    var PTZTmp = "%d";
    var BAUDRATE = "%d";
    var PTZSpeed = "%d";
    var SpeedDome_Type = "%d";
    var address = "%d";
    function onLoad(){
    ptzas.onload1(PTZTmp, PTZSpeed, address, SpeedDome_Type, BAUDRATE);
    }
    </script>
    </head>
    <body class="menu_color" bgcolor="#cccccc" onselectstart="return false;" onpaste="return false;" onLoad="onLoad();">
    <input name="right" type="button" onclick="ptzas.setAction(0)">
    </body>
    </html>
      

  6.   

    刚刚按xfsnero所说的修改了下还是不行还是谢谢你哟
      

  7.   

    'Ptz0'未定义
    'ptzas'为空或未定义
      

  8.   

    'Ptz0'未定义 
    'ptzas'为空或不是对象
    上面那写错了,呵呵
    帮我看下,谢谢了
      

  9.   

    我这里看不是'Ptz0'未定义(是不是你没加载到文件),'ptzas'为空或不是对象
    下面是修改过的,内容不全,无法继续调试<html>
        <head>
            <script type="text/javascript" src="ptz0.js">
            </script>
            <script language="JavaScript" type="text/javascript">
                var ptzas = new Ptz0();
                var PTZTmp = "%d";
                var BAUDRATE = "%d";
                var PTZSpeed = "%d";
                var SpeedDome_Type = "%d";
                var address = "%d";
                function onLoad(){
                    ptzas.onLoad1(PTZTmp, PTZSpeed, address, SpeedDome_Type, BAUDRATE);
                }
            </script>
        </head>
        <body class="menu_color" bgcolor="#cccccc" onselectstart="return false;" onpaste="return false;" onLoad="onLoad();">
            <input name="right" type="button" onclick="ptzas.setAction(0)">
        </body>
    </html>
    function Ptz0(){
        this.TiltUpP = "08";
        this.TiltDownP = "10";
        this.UpD = "08";
        this.DownD = "10";
        this.PTZProtocol;
        this.PTZ_SPEED;
        this.PROTOCOL_PELCO_P = 0;
        this.PROTOCOL_PELCO_D = 1;
        this.STX1 = "a0";
        this.AddrP = "00";
        this.ETX1 = "af";
        this.AddrD = "00";
        this.Synch = "ff";
        this.TiltPanSpeedMinM = "22";
        this.TiltPanSpeedMedM = "77";
        this.SpeedMinMikimi = "10";
        this.SpeedMedMikimi = "1f";
        this.speedome_type;
        this.baudrate;
        this.PTZSpeed;
        this.Address;
        this.PTZtmp;
        this.onLoad1 = function(ptztmp, ptzspeed, ADDress, speeddome_type, Baudrate){
            this.PTZtmp = ptztmp
            this.baudrate = Baudrate;
            this.speedome_type = speeddome_type;
            if (this.PTZtmp == 1) {
                this.PTZProtocol = this.PROTOCOL_PELCO_D;
            } else if (this.PTZtmp == 0) {
                this.PTZProtocol = this.PROTOCOL_PELCO_P;
            }
            this.PTZSpeed = ptzspeed;
            
            if (this.PTZSpeed == 1) {
                if (this.PTZProtocol == this.PROTOCOL_PELCO_D) {
                    this.PTZ_SPEED = this.SpeedMinMikimi;
                    
                } else if (this.PTZProtocol == this.PROTOCOL_PELCO_P) {
                    this.PTZ_SPEED = this.TiltPanSpeedMinM;
                }
            } else if (this.PTZSpeed == 2) {
                if (this.PTZProtocol == this.PROTOCOL_PELCO_D) {
                    this.PTZ_SPEED = this.SpeedMedMikimi;
                } else if (this.PTZProtocol == this.PROTOCOL_PELCO_P) {
                    this.PTZ_SPEED = this.TiltPanSpeedMedM;
                }
            }
            this.Address = ADDress;
            if (this.PTZtmp == 0) {
                if (this.Address != 0) this.Address = this.Address - 1;
            }
            if (address <= 0xf) {
                this.Address = "0" + this.Address.toString(16);
            }
            this.AddrP = this.Address.toString(16);
            this.AddrD = this.Address.toString(16);
        }
        this.Pelco_D_Message = function(action1, action2, panspeed, titlespeed){
            var checksum;
            if (this.speedome_type == 4) {
                checksum = (parseInt(this.AddrD, 16) + parseInt(action1, 16) + parseInt(action2, 16) + parseInt(panspeed, 16) + parseInt(titlespeed, 16) % parseInt("100", 16));
            } else {
                checksum = parseInt(this.AddrD, 16) + parseInt(action1, 16) + parseInt(action2, 16) + parseInt(panspeed, 16) + parseInt(titlespeed, 16);
            }
            while (checksum > 0xff) {
                checksum = checksum - 0xff;
            }
            if (checksum <= 0xf) {
                checksum = "0" + checksum.toString(16);
            }
            return '0x' + this.Synch + this.AddrD + action1 + action2 + panspeed + titlespeed + checksum.toString(16);
        }
        this.Pelco_P_Message = function(action1, action2, panspeed, titlespeed){
            var checksum = parseInt(this.STX1, 16) ^ parseInt(this.AddrP, 16) ^ parseInt(action1, 16) ^ parseInt(action2, 16) ^ parseInt(panspeed, 16) ^ parseInt(titlespeed, 16) ^ parseInt(this.ETX1, 16);
            if (checksum <= 0xf) {
                checksum = "0" + checksum.toString(16);
            }
            return "0x" + this.STX1 + this.AddrP + action1 + action2 + panspeed + titlespeed + checksum.toString(16) + this.ETX1;
        }
        this.sendCMD = function(CMDString){
            parent.frame2.Player.PtzControl(CMDString);
        }
        this.setAction = function(action){
            var PTZCmd;
            if (this.PTZProtocol == this.PROTOCOL_PELCO_P) {
                switch (action) {
                    case 0:
                        this.PTZCmd = this.Pelco_P_Message("00", this.TiltUpP, "00", this.PTZ_SPEED);
                        break;
                    case 1:
                        this.PTZCmd = this.Pelco_P_Message("00", this.TiltDownP, "00", this.PTZ_SPEED);
                        break;
                }
            }
            if (this.PTZProtocol == this.PROTOCOL_PELCO_D) {
                switch (action) {
                    case 0:
                        this.PTZCmd = this.Pelco_D_Message("00", this.UpD, "00", this.PTZ_SPEED);
                        break;
                    case 1:
                        this.PTZCmd = this.Pelco_D_Message("00", this.DownD, "00", this.PTZ_SPEED);
                        break;
                }
            }
            this.CMDString = "ptzcmd&baud=" + this.baudrate + "n81&cmd=" + PTZCmd;
            this.sendCMD(this.CMDString);
        }
    }
      

  10.   

    js文件加载到了的,我检查很懂遍了,而且我在系统里面加了以很简单的页面,然后也写了一个外部js文件,里面放了个类,运行却是正常的。。不晓得错哪里了...就这个东西搞了我几天了一直找不到原因
    高手们,帮帮我吧,谢谢啦