Results 1 to 5 of 5

Thread: confusion over scripts found in a book

  1. #1
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Angry confusion over scripts found in a book

    here is the site
    http://www.jsworkshop.com/js13examples.html

    the book is

    Sams Teach Yourself JavaScript in 24 Hours, 3rd Edition

    the problem is this code. some how the notes are name and called.the big problem is the Setup()function
    you write object tags for the divs. but the divs are not holding the sound. and they are not the file names so how do you write object tags for them and the sounds plays.

    why does this work?

    Code:
    function Setup() {
      if (!document.getElementById) return;
      // Set up event handlers and embed the sounds
      divs = document.getElementsByTagName("div");
      for (i=0; i<divs.length; i++) {
        // embed the appropriate sound using document.write
        document.write('<embed id="' + 'note_' + divs[i].id + '"');
        document.write(' src="' + divs[i].id + '.au" width="0" height="0"');
        document.write(' autostart="false" enablejavascript="true">');
        // set up the event handler
        divs[i].onclick = PlaySound;
      }
    }
    function PlaySound(e) {
      if (!e) var e = window.event;
      // which key was clicked?
      thiskey = (e.target) ? e.target: e.srcElement;
      var sound = document.getElementById("note_" + thiskey.id);
      try {
        // RealPlayer
        sound.DoPlay();
      } catch (e) {
        try {
          // Windows Media / Quicktime
          sound.Play();
        } catch (e) {
          alert("No sound support.");
        }
      }
    }
    // Run the setup routine when this script executes
    Setup();







    Code:
    .white {
      float: left;
      background-color: white;
      height: 300px;
      width: 30px;
      border: 2px solid black;
    }
    .black {
      float: left;
      background-color: black;
      height: 225px;
      width: 25px;
    }

    HTML Code:
    <body>
    <h1>JavaScript Piano</h1>
    <div class="white" id="c1"> </div>
    <div class="black" id="cs1"> </div>
    <div class="white" id="d1"> </div>
    <div class="black" id="ds1"> </div>
    <div class="white" id="e1"> </div>
    <div class="white" id="f1"> </div>
    <div class="black" id="fs1"> </div>
    <div class="white" id="g1"> </div>
    <div class="black" id="gs1"> </div>
    <div class="white" id="a1"> </div>
    <div class="black" id="as1"> </div>
    <div class="white" id="b1"> </div>
    <div class="white" id="c2"> </div>
    <p style="clear:left">
    Click the piano keys above to play sounds.
    </p>
    <script language="javascript" type="text/javascript"
      src="piano.js">  </script>
    </body>

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    you write object tags for the divs. but the divs are not holding the sound. and they are not the file names so how do you write object tags for them and the sounds plays.
    For each <div>, the script writes an <embed> with an ID of "note_" + the <div> ID and a filename of the <div> ID + ".au". So, for a <div id="g1">, the script will create an <embed id="note_g1" src="g1.au">.

    However, the quality of the code is shockingly poor. It uses global variables in completely spontaneous places, defined right underneath local ones, the old document.write() call instead of more standard DOM methods, completely invalid HTML (that's not even a whole document, let alone a valid one), pixel sizing, try/catch where simple object detection would suffice, and the <embed> element instead of the more standard <object>.

    In short, it looks like it's been written by a Java author who doesn't really understand Javascript very well but has decided to do something else for a hobby and thought he couldn't go far wrong. I'd abandon that book if I were you.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    okay but lets say the files were hosted on box.net you need the whole file name ravenshow. au avi. .mp3 whatever

    even if I was willing to use document.write() to get agound the appendElement stuff and or use try catch and I'm using <embed> and <object>

    could you write out all the codes to play what ever it is and show a message if your browser could support it.

    also if you add a new file the object and embed tags are already writen for it ,right?

    I'm asking this because i'm trying to use this to embed quicktime or WMP.
    I wanted to use try catch or a function on the codebace and classid like this.
    Code:
    document.write('CLASSID="'+reader()+ ' " ')
    
    function reader(){ 
    			
    try {document.write ("clsid:6BF52A52-394A-11d3B153-00C04F79FAA")};
    			catch(e){ 
     try { document.write ("clsid:02BF2505-8C17-4B23-BC80-D3488ABDDC6B")};
    			catch(e)
    {try{document.write ("clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA")};
    or
    Code:
    document.write('CLASSID="'try {document.write ("clsid:6BF52A52-394A-11d3B153-00C04F79FAA")};
    			catch(e){ 
     try { document.write ("clsid:02BF2505-8C17-4B23-BC80-D3488ABDDC6B")};
    			catch(e)
    {try{document.write ("clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA")};' " ')

  4. #4
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    heay i really need to understand how to embed things by the way of checking for plusins. I thoug this was one way to do it.

  5. #5
    Join Date
    Nov 2006
    Posts
    236
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    nevermind is there any good quicktime detection scripts out there.

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
  •