SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)

    Thumbs up eval() is not working in FireFox

    In the following piece of code, execution of eval() is not working in FireFox.
    Pls provide the suggestions.

    Code:
    var str="audioElement[0].play();";
    
    for(i=0;i<fileNames.length-1;i++)
    {
    			
    str = str + "audioElement[" + i + "].addEventListener('ended', function() {audioElement[" + (i+1) + "].play();}, false);";
    }
    
    str = str + "audioElement[" + (fileNames.length-1) + "].addEventListener('ended', function(){}, false);";
    eval(str);
    Here, filenames array is having the array of HTML5 audio tag elements.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Mr. Ram View Post
    In the following piece of code, execution of eval() is not working in FireFox.
    Pls provide the suggestions.
    "Eval is evil" - learn that and you won't go far wrong.

    Here's the code without the eval garbage.

    Code:
    function play(audio) {
        return function() {
            audio.play();
        };
    }
    
    audioElement[0].play();
    
    for (i = 0; i < fileNames.length - 1; i++) {
        audioElement[i].addEventListener('ended', play(audioElement[i + 1]), false);
    }
    audioElement[fileNames.length - 1].addEventListener('ended', function(){}, false);
    Last edited by paul_wilkins; Sep 16, 2012 at 16:41. Reason: fix capitalization issue
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    It's not working. Audio got executed for only first file.

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,150
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Perhaps due to a small typo.

    Quote Originally Posted by paul_wilkins View Post
    Code:
    for (i = 0; i < fileNames.length - 1; i++) {
        audioElement[i].addEventListener('ended', play(AudioElement[i + 1]), false);
    }
    This "a" should be lowercase.
    "First make it work. Then make it better."

  5. #5
    SitePoint Zealot Mr. Ram's Avatar
    Join Date
    Nov 2005
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Ya...! I have taken care.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •