Forum Moderators: open
In a separate js document . . .
// JavaScript Document
var d = Date()
if ((d.getHours() > 9) && (d.getHours() <= 23)){
callAudio();
} else {
document.write("Wrong Time");
}
function callAudio(){
var flashData = "
<script type=\"text/javascript\" src=\"../embed/swfobject.js\"></script> <div id=\"player\">Audio will play here</div>
<script type=\"text/javascript\">
var so = new SWFObject('../embed/player.swf','mpl','120','20','9');
so.addParam('allowscriptaccess','always');
so.addParam('allowfullscreen','false');
so.addParam('flashvars','&file=welcome_sm.mp3&title=My Description Here&frontcolor=000066&lightcolor=9999FF&screencolor=0000FF&autostart=true');
so.write('player');
</script>";
document.write(flashData);
}
Right now this produces NOTHING. I have even simplified it so it is only instructed to display text and that doesn't seem to work so I think there is some error in the logic. Any help would be greatly appreciated.
All my paths are accurate. I'm not sure if I am escaping characters like I need to in the flashData variable, but again, I think the real problem is before it gets to that point.
I am definitely not a javascript expert by any stretch of the imagination, so "thanks" in advance for help from all you experts :)
Start by simplifying even more...
var the_date = new Date() // create your date object
var the_hour = the_date.getHours()
if (the_hour > 9) {
alert('call the function')
}
else {
alert('don't call the function')
}
Then of course you will notice that the_hour reflects your visitor's current date and time for their time zone. This is probably not what you want...
You can then choose to convert to UTC or some such, or -preferably- to use your server time instead :)
I am now back to getting nothing when I try to add the content back in. I'm almost positive that it is just a stupid syntax error . . .
Anyway, this much works . . .
var the_date = new Date() // create your date object
var the_hour = the_date.getHours()
// I need both time checks for when I edit them for UTC time
if ((the_hour > 9) && (the_hour < 24)) {
alert('good time')
}
else {
alert('bad time')
} As soon as I try to add this function, the page fails (gives me nothing) - even before I actually call the function from the conditional statement. This is where I'm sure the syntax error must be causing the problem:
// function to actually create the Flash JW Player
function callAudio(){
var flashData = "
<script type=\"text/javascript\" src=\"http://www.example.com/embed/swfobject.js\"></script> <div id=\"player\">Audio will play here</div>
<script type=\"text/javascript\">
var so = new SWFObject('http://www.example.com/embed/player.swf','mpl','120','20','9');
so.addParam('allowscriptaccess','always');
so.addParam('allowfullscreen','false');
so.addParam('flashvars','&file=http://www.example.com/embed/welcome_sm.mp3&title=My Custom Text Here&frontcolor=000066&lightcolor=9999FF&screencolor=0000FF&autostart=true');
so.write('player');
</script>";
document.write(flashData);
}
I'm sure I am running into issues with the embedded script tags, or some double quotes that need to be single, or something stupid like that. I just can't figure it out . . .
eventually the conditional at the top will look like this:
if ((the_hour > 9) && (the_hour < 24)) {
callAudio()
}
else {
// do nothing
}
I have changed all the links to make them static to existing files on the web for anybody's testing purposes (these are not the ones I will be using in production but they will work for testing).
thanks again for any help!
[edited by: eelixduppy at 10:19 pm (utc) on Dec. 15, 2008]
[edit reason] exemplified [/edit]
To get the hours in utc, use getUTCHours()
the_hour will always have a value between 0 and 23, so you don't need the second part of your if statement.
For debugging purposes, you could create an external .js script that returns the_hour
Comment out the if..then statement and add
callAudio("It's " + the_hour + " right now")
the .js should be something like:
function callAudio(my_string) {
document.write(my_string)
}
Then, slowly add back the more complicated parts...
PS also check out the Venkman javascript debugger and Firebug
I guess I was hoping someone would quickly notice some glaring issue with my syntax. I'm sure it is enitrely in that string assigned to 'flashData'. I will proceed with breaking it down even more.
I left a true URL in there in case someone wanted to grab the code and test it on their local system.
Thanks again.