I have an HTML page with an iFrame inside and I want to send short string value from parent to the iFrame. The short string value is a file name like "voice2.mp3" and the iFrame uses WaveSurfer to play the audio file.
So far I check if the string value has changed using intervals on the iFrame side and, yes it works fine.
But I'm wondering how can I do this using postMessage and I have no idea how to implement it in my code?
Here is the iFrame code:
window.nameOld = "voice1.mp3";
setInterval(function(){
var name = parent.window.postMe;
if(name !== window.nameOld){
console.log("name Changed");
window.nameOld = name;
wavesurfer.load(name);
runFunc();
};
}, 1000);
var wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: '#edeaef',
progressColor: '#d3cbd8',
cursorColor: '#9800ff',
cursorWidth: 1,
interact: false,
responsive: true,
hideScrollbar: true,
});
function runFunc(){
wavesurfer.on('ready', function () {
wavesurfer.play();
});
wavesurfer.on('finish', function () {
console.log("Just finished!");
wavesurfer.pause();
});
}
And here is the parent code which will be executed on button click:
window.postMe= "voice2.mp3";