Click to See Complete Forum and Search --> : Is it possible for specified text to be a variable?


DarkScythe
12-14-2003, 06:56 PM
Hello everyone, this is my first post here.
Just stumbled onto this forum via google, looking for a javascript forum, and I hope this place is friendly ^^;

Anyway, I'm an utternewb to javascript, and have been racking my brains over this, what should be a very simple script, for over a month now. I've read over large portions of a Javascript bible and asked everyone I know, and several other forums (albeit, not specifically webdev) and everyone was baffled.

My first thing is.. is this even possible with Javascript?

Here's the background:
I am creating a blog, but to set an atmosphere for the reader, I wish to have a MIDI playing in the background appropriate to the mood of the entry.
I asked myself how to go about this, and came up with the following:
I'll start off with a Mood, and set whatever text that follows as a variable. (This is my main problem as of now)
Whatever text there will trigger one MIDI from a list and write an embed code for it into the document and play it.

It seems simple, but for some reason it will not work no matter what I try. I have stamped out all syntax errors, and have come up with this. (To keep things neat, I split up the code into the base HTML code that calls up a separate *.js code in the header)

The HTML says:
Current Mood: <span id="mood">Happy</span>
This is in the body, there is a script tag in the header that calls a JS file in.

The Javascript has this:
var mood = document.getElementById('mood')
document.write("currently set mood is " + mood +"<br><br>")
switch (mood)
{
case "Sad":
document.write("<embed src='http://www.darkscythe.com/bgm/ccs_its-my-life.mid' loop='999' autostart='true' hidden='true'>")
break
case "Happy":
document.write("<embed src='http://www.darkscythe.com/bgm/bgm18.mid' loop='999' autostart='true' hidden='true'>")
}


Hopefully this isnt too long, but where I told it to write "currently set mood is.." it will say "currently set mood is null"

I'm guessing this is a problem with the code not being able to recognize what is directly after Mood in the html, defined between the SPAN tag. Someone suggested I use the .innerHTML thing, but I have no idea how to use it...

Can anyone else take a crack at it?

ray326
12-14-2003, 07:16 PM
Try something like this untested hack:

var mood = document.getElementById("mood").innerHTML

DarkScythe
12-14-2003, 11:38 PM
thanks for the suggestion, I tried it, but it didnt seem to do the trick.. :(

Actually, where it said currently set mood is null before, now that line has disappeared, and once again the script has an error.

It gives me an Object Required (Line 3, Char 1, Code 0) message..

Altho I can't figure out what is missing from L3,C1,C0 (of the main HTML page)

Either we're not using that attribute correctly or something is wrong.. see why i've been distressing over this script for over a month? lol it seems stubborn to work.

Pittimann
12-15-2003, 12:26 AM
Hi!

If you really followed ray326's advice and the stuff doesn't work, I only see one reason for it. The script already does its' job, before the document body is entirely loaded. So please try putting your code in a function and call it onload of the body:

function setMood(){//between the opening and closing script tag
var mood = document.getElementById("mood").innerHTML
document.write("currently set mood is " + mood +"<br><br>")
switch (mood)
{
case "Sad":
document.write("<embed src='http://www.darkscythe.com/bgm/ccs_its-my-life.mid' loop='999' autostart='true' hidden='true'>")
break
case "Happy":
document.write("<embed src='http://www.darkscythe.com/bgm/bgm18.mid' loop='999' autostart='true' hidden='true'>")
}
}

In your body tag you add somethin like:
<body onload="setMood();">

That should do the job...

Cheers - Pit

DarkScythe
12-15-2003, 11:19 PM
hmm, you know I've actually never thought of it that way!

I always assumed there was something wrong with the code itself, rather than the script finishing its job before the body loaded..
Now that I think about it, it makes sense because the header is always executed first..but it never occurred to me.

Anyway, I tried your suggestion on making it a function, and after a bit of modifying (a fe troubles here and there) i managed to get it to work.. however, oddly enough I had to change the EMBED tags to BGSOUND tags.

I can't quite figure out why the embed tag won't work. I get no error messages, and on view source, the embed tag is written there, but it won't play..
Change it to bgsound and everything works fine, except for two things:
1) the status bar says downloading data from <midi URL> and stays there..
2) the reason I don't like bgsound too much is the midi pauses when the window is minimized. I like the embed because it plays in the background even if you do minimize it.

I'll try to find a workaround for this, but help on it would be appreciated.

Much thanks for your help in getting it off the ground at least, ray and pit :D