Jump to content

Play sound one time - not exiting


thesoundsmith

Recommended Posts

I want the home page at http://www.thesounds....com/index.html to play a brief piano phrase the first time a viewer enter the page, subsequent reloads to not play the file until the viewer closes the browser and ends the session. I don't want to use Session info, it's not ready across all platforms. I used a set of functions that work fine in a test page (http://www.thesounds...acookietest.htm but apparently either do not create the cookie or don't age it properly in the actual page. I don't see where the difference is, any ideas appreciated.

<script type="text/javascript" src="smgr/script/soundmanager2.js"></script><script type="text/javascript">function getCookie(NameOfCookie){	if (document.cookie.length > 0) {	begin = document.cookie.indexOf(NameOfCookie+"=");	if (begin != -1) {	  begin += NameOfCookie.length+1;	  end = document.cookie.indexOf(";", begin);	  if (end == -1) end = document.cookie.length;		return unescape(document.cookie.substring(begin, end));	}  }  return null;}function setCookie(NameOfCookie, value, expiredays){ var ExpireDate = new Date ();ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));document.cookie = NameOfCookie + "=" + escape(value) +((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());}function DoTheCookieStuff(){visited=getCookie('visited');if (visited==null){setCookie('visited','yes',0)soundManager.setup({  url: 'smgr/swf/',  onready: function() {   var mySound = soundManager.createSound({	  id: 'aSound',	  url: 'wavs/memintro.mp3'	});	mySound.play();  },  ontimeout: function() {	// Hrmm, SM2 could not start. Missing SWF? Flash blocked? Show an error, etc.?  }});}}</script>   <!-- SMGR setup --></head><body onLoad="DoTheCookieStuff()">

If I remove the cookie, the sound plays every time at http://www.thesoundsmith.com/indexOLD.html Just to make matters more interesting, after a couple test runs, my FTP functions either slow to a crawl or stop entirely, I am afraid something in the script is looping or messing with the server. Eventually it runs down, but ??? Again, any thoughts much appreciated.

Edited by The Soundsmith
Link to comment
Share on other sites

Thanks, justsomeguy, I thought zero left the cookie alive till the browser closed. Changed the code to

setCookie('visited','yes',null)

and now Chrome and IE work correctly, but for some reason FireFox is not playing the sound. I'm hoping it's a cache issue, but I don't see the cookie on my machine.Solved: changed the cookie name and now everybody is happy. Thanks for the help.

Edited by The Soundsmith
Link to comment
Share on other sites

0 will tell it to be a temporary cookie, but your setCookie function will set an expiration date to the current date plus whatever is passed, unless you pass a null value (then it will not set an expiration). That's what this is doing: document.cookie = NameOfCookie + "=" + escape(value) +((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString()); So the cookie will never include an expiration of 0, it will either be left off or it will be set to the current time plus the given number of days.

Link to comment
Share on other sites

Yeah, it's not really "my" function, and i tried to simply pass null - and failed - but leaving it aloine and passing null in the call worked (I don't do enough js to know what I'm doing, I work from example. My grammar is worse than Red Riding Hood's...) Anyway, it's fine now, thanks..

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...