Computing.Net > Forums > Web Development > Autoplay audio once per visit

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Autoplay audio once per visit

Reply to Message Icon

Name: clomper
Date: May 24, 2006 at 09:55:36 Pacific
OS: Win XP Home
CPU/Ram: 3.0/1.0
Product: Sony
Comment:

I have an audio clip that plays via the EMBED tag on a webpage. Here's the code:

<EMBED SRC="audio/message.mp3" AUTOSTART="true" VOLUME="50" HEIGHT="60" WIDTH="144" HIDDEN="TRUE">

It play once and stops, which is what I want, however, if a visitor goes to another page in the site, then returns to the audio-embedded page, the sound plays again. Is there any way to play the sound only once per visit to the site? Perhaps by using a cookie? Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: May 24, 2006 at 11:45:12 Pacific
Reply:

Yes and no. If you only want the clip to play once during a visit to the page, then what you want to use is session variables. You would need to have some type of back-end support on the web server (such as PHP or VBScript) to accomplish this - at least I don't think you can do it with client-side JavaScript.

You could use cookies to limit the clip from playing for specified time periods though. For example, the page checks to see if the cookie has been set. If the cookie has not been set (or if it was set more than 24 hours ago) it plays the clip and set a cookie. If your server supports a back-end scripting language, such as PHP or VBScript, that would still be the best approach for this method as well. However, if you don't have that cabability you can still do this with client-side JavaScript

Michael J


0

Response Number 2
Name: clomper
Date: May 24, 2006 at 14:04:27 Pacific
Reply:

I like the cookie approach because it seems less complicated. Besides, my visitors won't be spending a ton of time on the site, so if I could set a cookie that indicates whether or not the sound has been played in the last hour, that would be fine. I don't suppose you'd be willing to share an example of such code with me? I'm no JavaScript expert, heck, I'm not even a JavaScript beginner, so I sure would appreciate any help you can give me. Thanks.


0

Response Number 3
Name: Michael J (by mjdamato)
Date: May 26, 2006 at 08:30:19 Pacific
Reply:

Here is a page with JavaScript functions that allow you to set, read and delete cookies. Include that code in the head of your page.

Then where you would put the EMBED tag do this (I have not tested it):

<script language="javascript">
if (!getCookie('lastClip')) {
//Include the clip
document.write('<EMBED SRC="audio/message.mp3" AUTOSTART="true" VOLUME="50" HEIGHT="60" WIDTH="144" HIDDEN="TRUE">')
setCookie('lastClip',true,3600000)
}
</script>

The number above in red is the time (in milliseconds) for the cookie to expire. The time I put there is for 1 hour (1000ms * 60sec * 60min). Change it for whaever amount of time you want.

I also ran across some examples concerning session cookies for javascript. That would probably be an even berrter handling of this.

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

Michael J


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Autoplay audio once per visit

javascript menu www.computing.net/answers/webdevel/javascript-menu/1696.html

Clear Visited Link Status www.computing.net/answers/webdevel/clear-visited-link-status/1855.html

cant access webpage outside lan ..? www.computing.net/answers/webdevel/cant-access-webpage-outside-lan-/3045.html