Calling SM2 from 2nd Javascript function
I have set up SM2 and everything is working PERFECTLY. I have a sound that plays on loading the page, which can also be toggled. A second sound responds to mouseover/out elsewhere on the page.
I would like to set up SM2 to play only once per session. I have a working cookie detect.create script, but I just cannot get this 2nd script to call SM2 to play onload (1st time roun, i.e. no cookie).
Here is the code I am using for SM2:
And the code for the cookie finder/creator:
I have also tried combinations using GetElementbyID, createSound.
The sounds load fine, as they still respond to mouse events, but I just cannot get SM2 to play based on this cookie script.
PS
I am not java programmer, so the verious website that delve into java theory just give me a headache!!
Surely there is a simple call function/syntax that will call the play function of SM2 from within this script?
Thanks in advance.
And great product, congrats to the development team :o)
PAUL
I would like to set up SM2 to play only once per session. I have a working cookie detect.create script, but I just cannot get this 2nd script to call SM2 to play onload (1st time roun, i.e. no cookie).
Here is the code I am using for SM2:
// SM2
soundManager.url = 'audio/';
soundManager.debugMode = false;
soundManager.onload = function() {
var mySound = soundManager.createSound({
id: 'sound',
url: 'audio/eagle.feather.mp3',
volume: 80,
autoPlay: false
});
And the code for the cookie finder/creator:
// Run once per Session
var key_value = "myTestCookie=true";
var foundCookie = 0;
// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');
// Walk through the array
for(var i=0;i < cookieArray.length;i++)
{
var checkCookie = cookieArray[i];
// Remove any leading spaces
while (checkCookie.charAt(0)==' ')
{
checkCookie = checkCookie.substring(1,checkCookie.length);
}
// Look for cookie set by key_value
if (checkCookie.indexOf(key_value) == 0)
{
soundManager.play('sound');
// alert("Found Cookie");
// The cookie was found so set the variable
foundCookie = 1;
}
}
// Check if a cookie has been found
if ( foundCookie == 0)
{
// The key_value cookie was not found so set it now
document.cookie = key_value;
// alert("Setting Cookie");
}
I have also tried combinations using GetElementbyID, createSound.
The sounds load fine, as they still respond to mouse events, but I just cannot get SM2 to play based on this cookie script.
PS
I am not java programmer, so the verious website that delve into java theory just give me a headache!!
Surely there is a simple call function/syntax that will call the play function of SM2 from within this script?
Thanks in advance.
And great product, congrats to the development team :o)
PAUL
1
person has this question
I have this question, too!
Tell me when someone answers.
The more people who ask this question, the more it gets noticed.
The more people who ask this question, the more it gets noticed.
Create a customer community for your own organization
Plans starting at $19/month
-
Inappropriate?Your JavaScript (not quite Java, mind you! :P) for SM2 looks okay, perhaps your cookie-related JS isn't quite right - I'm not certain, but I might take a look at some of the free "set cookie" and "read cookie" scripts out there.
There is one timing issue with your cookie code, which is going to run "right away" and before soundManager.onload() has fired, so this may be causing problems as SoundManager won't be able to play sounds yet.
What you basically want, if I read your request correctly, is to check the cookies only after SoundManager is ready itself. It will then try to play a sound just the first time.
soundManager.onload = function() {
var mySound = soundManager.createSound({
id: 'sound',
url: 'audio/eagle.feather.mp3',
volume: 80,
autoPlay: false
});
if (!get_cookie('testCookie')) {
// 'testCookie' has not been set, user hasn't been here this session
// so we can make noise
// the createSound call could go here too, instead..
mySound.play();
// and set the cookie too
set_cookie('testCookie',true);
} else {
// the cookie DOES exist, etc.
//
}
}
If your cookie code doesn't have getter and setter-type functions, I did a quick search and found something along these lines:
http://www.mach5.com/support/analyzer...
I can't provide much help on the cookie end as this is a SoundManager 2 support forum, but you are definitely on the right path!
-
Inappropriate?It's possible that the cookie script is being called before SoundManager is loaded and therefore has not created the sound before the cookie script tries to call it.
Here is a script for cookies that I came up with some time ago that may help:
var C=Cookies= {
started:false,
values:{},
path:"\/",
create:function (name,value,days,path) {
if (days){
var date= new Date();
date.setTime(date.getTime()+((days||365)*24*60*60*1000));
var expires = date.toGMTString();
}
document.cookie = name+"="+escape (value )+(!days? "" : "; expires="+expires)+";path="+(path||this.path);
this.values[name]=value;
},
modify:function (name,value,days,path) {
this.create (name,value,days,path);
},
init:function () {
this.path = location.pathname.substr(0,location.pathname.lastIndexOf("\/")+1);
var allCookies = document.cookie.split ('; ');
for (var i=0;i<allCookies.length;i++) {
var cookiesPair = allCookies[i].split('=');
this.values[cookiesPair[0]] = unescape(cookiesPair[1]);
}
this.started=true;
},
read:function (name) {
return this.values[name];
},
erase:function (name) {
this.create(name,"",-1);
this.values[name]=null;
}
}
Cookies.init()
Usage:
Cookies.create("mycookie","cookievalue"); //session only
var value = Cookies.read("mycookie");
OR
Cookies.create("mycookie","cookievalue", numberofdays); //set number of days
To use it for your website I would suggest something like this:
soundManager.url = 'audio/';
soundManager.debugMode = false;
soundManager.onload = function() {
var mySound = soundManager.createSound({
id: 'sound',
url: 'audio/eagle.feather.mp3',
volume: 80,
autoPlay: false
});
if (Cookies.read("myTestCookie")!="true"){
mySound.play();
}else{
Cookies.create("myTestCookie","true");
}
}
This way you can tell the sound to play AFTER it's been created.
Cheers,
bum51
I’m confident
1 person says
this answers the question
-
There seems to be an error in the last part of your script, I think it needs to read:
if (Cookies.read("myTestCookie")!="true"){
mySound.play();
Cookies.create("myTestCookie","true");
}
Otherwise no cookie ever gets created! No need for an if/else statement.
Once I made this change it works a dream - thnx again :o)
} -
haha, woops! -
Inappropriate?Yes you guys are 100% spot-on.
Strangely, immediately after making the post I figured that may be the problem and fixed it, albeit in a clumsy way.
I incorporated the SM2 onload script into the cookie script, and it now works perfectly. It was the timing!
However, I do think it is preferable to have the cookie script put in the other way round, as you both have suggested above. This makes for more compact and intuitive code.
THANKS A BUNDLE GUYS!
It really is great I can come here and get answers to tricky problems (for me, at least) so quickly. Very, very much appreciated.
Be well,
PAUL -
Inappropriate?bum51 would you like a credit and link from my site once its up? I like to credit people who have helped me out with content, I think that's fair :o)
Will also be a link to SM2 website too!
I’m delighted
-
Inappropriate?Hahaha, sure that'd be great! But for the sake of mild anonymity, I'd like credit as either "bum51" or "Steak" (if you don't want to put "bum51" on your site ;-) ) no real names for me :)
I’m excited
Loading Profile...



EMPLOYEE
