代码之家  ›  专栏  ›  技术社区  ›  EmmyS

javascript错误:未定义对象

  •  0
  • EmmyS  · 技术社区  · 14 年前

    我正在尝试重用我在另一个页面上使用的一些脚本,但是这个脚本总是出错。

    我有一个名为CalendarPopup.js的.js文件。它像这样链接到我的HTML:

    <script type="text/javascript" src="/administrator/components/com_arrcard/js/CalendarPopup.js"></script>
    

    这是一个javascript库,它显示一个弹出式日历,用户可以在单击链接时从中选择日期。然后将值放入字段中。下面是显示字段和链接的代码:

    <tr id="birthDate">
    <td align="left" valign="top">
        <script type="text/javascript">
        var calStart = new CalendarPopup("calendar");
        calStart.showNavigationDropdowns();
        </script>
        Birth Date:
    </td>
    
      <td>
        <input style="width:124px;" type="text" name="birthdate" value="" />
        <a href="#" onclick="calStart.select(document.instantForm.birthdate,'anchorBirthdate','MM/dd/yyyy'); return false;" id="anchorBirthdate">select</a>
      </td>
    </tr>
    

    然后有一个div实际保存日历:

    <div id="calendar" style="position:absolute;visibility:hidden; background-color:white; layer-background-color:white;"></div>
    

    所有这些在一个页面上都完美无瑕,但在这个新页面上,我得到了错误

    CalendarPopup is not defined
    

    在这条线上:

    var calStart = new CalendarPopup("calendar");
    

    我错过了什么?我肯定这是很明显的,但我盯着它看太久了,就是看不见。

    4 回复  |  直到 14 年前
        1
  •  1
  •   xyz    14 年前

    确保定义CalendarPopup类的脚本在任何其他javascript引用它之前都在执行它的位置。

        2
  •  1
  •   Lee Brindley    11 年前

    您可以在窗口中添加一个事件侦听器,并在加载.js文件后执行使用这些文件的代码。

    //Some js file
    
    window.addEventListener('load',onload,false);
    
    
    function onload(){
    
     var x = new yourObject();
    }
    

    您当前在加载之前调用它,因此对象未定义错误。

        3
  •  0
  •   Nazmul    14 年前

    如果js文件的源与 http://www.mattkruse.com/javascript/calendarpopup/source.html ,然后据说还必须包括其他三个js文件
    锚定位置.js
    日期.js
    弹出窗口.js

    谢谢。

        4
  •  0
  •   Insyte    11 年前

    问题很可能是javascript文件的加载顺序。

    放置 <script> 中的包含标记 <head> 你会没事的。