外部JavaScript文件

时间:2017-04-02 07:40:41

标签: javascript function external

我正在使用JavaScript开发一个声音库。我创建了sound.js文件,这个文件包含在test.php文件中。 sound.js文件包含一些功能。我的sound.js文件是这样的:

var soundPath   = './sound/';

/*
 * Sounds Path
*/
var phoneNumberSound        = ['0.wav', '1.wav', '2.wav', '3.wav', '4.wav', '5.wav', '6.wav', '7.wav', '8.wav', '9.wav'];
// var phoneCallSound          = null;

/*
 * Play Phone Number's Sound
*/
function playPhoneNumberSound (phoneNumber)
{
    phoneNumber = parseInt(phoneNumber);
    if (phoneNumber.length > 1){
        var phoneNumber = phoneNumber.substring(phoneNumber.length - 1);
        new Audio(soundPath + phoneNumberSound[phoneNumber-1]).play();
    } else {
        new Audio(soundPath + phoneNumberSound[phoneNumber-1]).play();
    }
}

/*
 * Coming Phone Incoming Sound
*/
function playPhoneIncomingSound ()
{
    document.writeln('Test this func');
}

在test.php中,我调用上面的函数。第一个功能正在运行,但第二个功能或第三个或其他功能......不起作用。浏览器错误结果如下:

Uncaught ReferenceError: playPhoneIncomingSound is not defined

我的test.php文件是这样的:

<script src="lib/jquery.js"></script>
<script src="lib/sound.js"></script>

<input type="text" id="phoneNumber" />
<button id="playSound">Play Sound</button>

<script type="text/javascript">

    $(document).ready(function ()
    {
        $('#playSound').click(function ()
        {
            playPhoneIncomingSound ();
            // var phoneNumber = $('#phoneNumber').val();
            // playPhoneNumberSound(phoneNumber);
        });
    });
</script>

为什么呢?为什么会出现此错误?我的错误在哪里?

感谢!

修改 关于Chrome的此错误。当我清除浏览数据时,此错误将丢失。在firebox中,我没有像这样的错误!

1 个答案:

答案 0 :(得分:0)

为什么不使用onclick拨打playPhoneIncomingSound()

&#13;
&#13;
var soundPath = './sound/';

/*
 * Sounds Path
 */
var phoneNumberSound = ['0.wav', '1.wav', '2.wav', '3.wav', '4.wav', '5.wav', '6.wav', '7.wav', '8.wav', '9.wav'];
// var phoneCallSound          = null;

/*
 * Play Phone Number's Sound
 */
function playPhoneNumberSound(phoneNumber) {
  phoneNumber = parseInt(phoneNumber);
  if (phoneNumber.length > 1) {
    var phoneNumber = phoneNumber.substring(phoneNumber.length - 1);
    new Audio(soundPath + phoneNumberSound[phoneNumber - 1]).play();
  } else {
    new Audio(soundPath + phoneNumberSound[phoneNumber - 1]).play();
  }
}

/*
 * Coming Phone Incoming Sound
 */
function playPhoneIncomingSound() {
  document.writeln('Test this func');
}
&#13;
<button id="playSound" onclick="playPhoneIncomingSound()">Play Sound</button>
&#13;
&#13;
&#13;

在你的test.php代码中,playPhoneIncomingSound ();试图在脚本标记代码中找到该函数,但不在sound.js中找到