I have made this script for my forum and I was wondering if anyone could guide me how to get the browser to play an audio file if the user hovers over the class style113
, but it has to give them a warning alert that says "audio is about to play"
if they press ok on the alert it should play if they press cancel it should not. How can I achieve this? Here is the script I have so far:
my script has been removed
You can use the following code:
var elems = document.getElementsByClassName("style113");
for(i in elems) {
elems[i].addEventListener("mouseover", function() {
if(confirm(" %%% CONFIRMATION MESSAGE %%% ")) {
// %%% CODE TO PLAY SOUND %%%
}
});
}
What it does:
style113
mouseover
confirm()
popup (has two buttons, one to confirm and one to cancel) confirm()
method returns true
(if the positive button is clicked), then play sound UPDATE As per OP request in the comments below, you can add this code to your specific code in the for
loop:
document.getElementsByClassName('style113')[x].addEventListener("mouseover", function() {
if(confirm("audio is about to play")) {
// %%% CODE TO PLAY SOUND %%%
}
});
I'd also advise you to clean up your source code with better indenting practices. Also, avoid making too many DOM requests (eg, repetitive document.getElementsByClassName()
) and look instead to caching DOM requests .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.