Click here to Skip to main content
15,916,019 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I want to create an image that changes permenantly on mouseover to another image and plays a sound. Once the image changes I do not want the image or sound to play again. Is there a way to do this? The code I am using it based on here and here and I have managed to get the image to change and play a sound, but it still plays the sound even when the image has changed.

Thanks in advance guys! :)
Posted
Updated 25-Sep-15 11:26am
v2

In addition to Solution 2. As using global objects is arguably considered to be bad programming practice, I could advice more object-oriented approach, specific to JavaScript. You can keep the flag "soundAlreadyPlayed" in a DOM element itself used to trigger the event handled with your sound. For example:
JavaScript
var myImage = document.getElementById("image");
myImage.onmouseenter = function() {
    if (!myImage.soundAlreadyPlayed) {
        // play your sound here
        myImage.soundAlreadyPlayed = true;
    }
}
It will work because the property soundAlreadyPlayer is undefined for the elements, so the condition under "if" will return true at first. On next moves of the mouse onto the element, the handler won't do anything, because the property is defined and returns true.

Please see:
https://developer.mozilla.org/en-US/docs/Web/Events/mouseover[^],
https://en.wikipedia.org/wiki/JavaScript#Dynamic[^].

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 26-Sep-15 4:26am    
Interesting, 5ed.
Sergey Alexandrovich Kryukov 26-Sep-15 5:42am    
Thank you, Afzaal.
—SA
Yes, well I am not sure why you still didn't get the solution when you have explained it properly in your question. :-)

1) Create a global variable and name it something like, playSound etc.

JavaScript
var playSound = true;


2) Once you have changed the picture play the sound using a condition,

JavaScript
// Change the image
if(playSound) {
   // play the sound
   // Now change the variable to false
   playSound = false;
}


3) Now try again, it won't play sound because the function to play the sound is in a condition. The condition is controlled by the variable above. So once you change it to false, it won't play the sound again.

Another solution I am thinking about uses the src element of the image to determine whether to play the sound or not. But that will be a little bit complex, so use the one shown above.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Sep-15 20:52pm    
I voted 4 this time. There is solution better than using global variables, which is arguably considered to be a bad programming practice. Please see Solution 4.
—SA
Afzaal Ahmad Zeeshan 26-Sep-15 4:27am    
Thank you, Sergey.

Yes, as I mentioned there are many ways of doing so and I find yours a better one.
One easy way to do this is use as css class to indicate something has occurred before or not.

Something like:

// css
.hasPlayed{
}

// js
// assume you're using jQuery here and the id of the elem is "picture1"


if( !$("#picture1").hasClass('hasPlayed') ){

// code to play the song or whatever action here

// and then...
$("#picture1").addClass('hasPlayed');
}
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 25-Sep-15 18:29pm    
Good, but you missed the main point. He needs to use JavaScript (or jQuery) to do that. CSS cannot handle client-side requests, it is just a stylesheet.

He needs to determine whether to play the music or not, even in that case (where he uses CSS classes or IDs) he needs JavaScript.
[no name] 25-Sep-15 19:22pm    
Good, but you missed my main point - "One easy way to do this...."



Afzaal Ahmad Zeeshan 26-Sep-15 4:12am    
But that doesn't do this. How would it play the sound in the first place? CSS cannot do that. :-)
[no name] 26-Sep-15 12:12pm    
I wasn't writing the whole function for him as he already mentioned he had code based on other articles to do this. This is why I added the line:

// code to play the song or whatever action here

Your flawed assumption and inability to understand what I wrote by the very fact that I did not in any way indicate that CSS would play the sound is just that - flawed. It is important that one have the ability to understand something when they read it and comprehend simple examples and matters.

I think you missed the boat on the idea I was conveying Afzaal - but that's ok as I see you also missed the point of the commented line in the example that left it to the original person to insert whatever they wanted there to play a sound.
So Afzaal, you complain that using my method he needs Javascript - and then you turn around and post a solution using Javascript :-)

By the way, my method does not require a global variable polluting the global namespace....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900