Basically I have different background sounds for different places, with a variable associated with each sound (though I think I could actually do with one variable, which would be simpler).
At a passage where you may come from a different place, I have it so that if that variable is 1 then it fades the sound out and sets the variable to 0.
At first when I would load a saved game, no sound would play until you went to a different passage where it was supposed to switch sounds. But when I added just this to the Javascript, the sound would play:
Config.saves.onLoad = function (save) {
$.wiki('');
};
But then the problem was, because it did not save the variable change to 0, the one background sound would start at full volume and then fade out, which of course sounds very cheap.
So I used HiEv's suggestion to save the variable changes, but then the sound was silent again, because I have them start playing at volume 0 in StoryInit instead of trying to get them to start looping at all the first passages you might encounter them in. So I used this:
Config.saves.onLoad = function (save) {
$.wiki('<<goto "load">>');
};
And in "load" I put:
<<set $wa to previous()>><<goto $wa>>
And then I could put any macros I wanted into the "load" passage and they would run only when the game was loaded, so I put in <<if>> macros saying to fade in a background sound if its variable was 1.
That may have been an absurdly roundabout workaround, but it works for what it's worth!
Chapel's answer does look easier, and I will certainly try it first in my future adventures.