Search code examples
javascriptfunctionaudiocall

Javascript function calls not working in IE & Chrome


When I call the following functions nothing happens, when I use them in in-line js they working fine. Am I doing something wrong?

 <script>

    var muted = false;
    function playAudio(var audioFile)
    {
        var audio = document.getElementElementId(audioFile);
        audio.play();
    }
    function toggleMute(var audioFile)
    {
        var audio = document.getElementById(audioFile)
        if(muted)
        {
            audio.play();
        }
        else
        {
            muted = !muted;
            audio.pause();
        }
    }
</script>

and the calls:

<button type="button" onclick="toggleMute('bg_music')">mute</button>

however when I use in-line JS like below it works fine.

<area shape="poly" coords="232,110,246,119,233,141,219,134,232,110
onclick="document.getElementById('ipod').play()">

Am I doing something wrong when I call these functions?


Solution

  • The problem is not in how you're calling the functions, but in how you're declaring them. You don't need to include the var keyword in a parameter list.

    Try this:

    function playAudio(audioFile)
    {
        ...
    }
    
    function toggleMute(audioFile)
    {
        ...
    }
    

    Further Reading