PDA

View Full Version : How to play intro music only once?


Forestc
08-09-2002, 05:59 PM
Hi,
I have a MIDI file that plays EVERY time you visit the homepage by using: (I've hidden it for now, till I fix this...)
<!--embed src="nyny.mid" autostart="true" width=145 height=60 border=1 loop="off"> -->

How can I make this play only once, even if the user clicks a link and returns to the homepage?

I've seen it done with javascript for submit buttons in a form, made it clickable only once.

Please let me know if you need more info.
Thanks:)
Forest

ConfusedOfLife
08-09-2002, 07:29 PM
Well, if the user clicks on a link on your page and the link is opened in the current page, I mean not a blank new page, then I think you have to use cookies, that when the same user tries to visit the main page again, you find it out by means of cookies and you do not play your midi file, but if he is a new person, that means that he never was in your homepage, then the midi get's excecuted. If the cookie says that he never was, then you can run your middi by this line, assuming that the name of the div's id that you wana have your <embed> in is "midiPlayer" :

document.getElementById("midiPlayer").innerHTML =
"<embed src='song.mid' autostart='true' loop='false' \
height = 0 width = 0>";

Mr J
08-16-2002, 08:47 PM
Try the following after changing sound.mid to the name of your sound file.


<html>
<head>
<title></title>
<SCRIPT LANGUAGE="JavaScript">
<!--
var expDays = 1; // number of days the cookie should last

function dothis(){
document.embeds[0].play()
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
}
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function checkCount() {
var count = GetCookie('count');
if (count == null) {
count=1;
SetCookie('count', count, exp);

dothis()

}
else {
count++;
SetCookie('count', count, exp);
}
}
// End -->
</script>

<EMBED SRC="sound.mid" loop=false autostart=false hidden=true mastersound>
<BODY OnLoad="checkCount()">

</body>
</html>

newwebsite
12-08-2005, 12:19 PM
i want to put music in my new website. there are website offering free music video codes but i don't want a video. i just want a music to be a background of my page. how can i put music saved on my documents as background. thanks.:)

PhotoJoe47
12-08-2005, 07:29 PM
Hi,
I have a MIDI file that plays EVERY time you visit the homepage by using: (I've hidden it for now, till I fix this...)
<!--embed src="nyny.mid" autostart="true" width=145 height=60 border=1 loop="off"> -->

How can I make this play only once, even if the user clicks a link and returns to the homepage?

I've seen it done with javascript for submit buttons in a form, made it clickable only once.

Please let me know if you need more info.
Thanks:)
Forest

Like a couple of other have said you can use a cookie to control if the music played or not.

Do you want to have user only here the music the first time they visit your site per session? What I mean by that you can have two different types of cookies. One you can set how long you want the cookie to stay on the person's computer or two a session only cookie. The session cookie is only good until they close their browser program, then it is gone. I think most user disallow the the first type but allow the second. Also with the second cookie type, called session cookie, your vistors will hear the music the first time they come to your home page every session. So what I'm trying to say is I think session cookie would be best for this problem.

If you are new to session cookies here is a link to a good tutorial.

http://www.javascriptkit.com/javatutors/cookie.shtml

On the second page there is a link you can click on to get a zip file with a set of cookie functions. I think they are quite good, with lots of notes to help you understand them.