PDA

View Full Version : confusion over scripts found in a book



riptide
05-04-2007, 07:02 PM
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?


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();









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




<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>

Twey
05-04-2007, 07:42 PM
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.

riptide
05-04-2007, 08:12 PM
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.


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
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")};' " ')

riptide
05-05-2007, 08:21 PM
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.

riptide
05-07-2007, 04:31 PM
nevermind is there any good quicktime detection scripts out there.