The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Image hover/rollover/mouseover: back button breaks mouseout
Discuss Image hover/rollover/mouseover: back button breaks mouseout in the JavaScript Development forum on Dev Shed. Image hover/rollover/mouseover: back button breaks mouseout JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 7th, 2013, 11:55 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 40 m 24 sec
Reputation Power: 0
|
|
|
Image hover/rollover/mouseover: back button breaks mouseout
Please see the three image links in the middle of the page: http://spncolors.com/ . If you hover over and then click, you're taken to the appropriate page. But now click the back button and the image stays frozen in its "mouseover" state, rather than reverting to the "mouseout" state. Firefox and Safari have both shown this problem. Chrome seems to be okay however.
I'm a total amateur here, so any help is appreciated. Here is my code:
Code:
Code:
jQuery(function($) {
$(document).ready(function() {
// Preload all rollovers
$("#hoverlink img").each(function() {
// Set the original src
rollsrc = $(this).attr("src");
rollON = rollsrc.replace(/.jpg$/ig,"-over.jpg");
$("<img>").attr("src", rollON);
});
// Navigation rollovers
$("#hoverlink a").mouseover(function(){
imgsrc = $(this).children("img").attr("src");
matches = imgsrc.match(/-over/);
// don't do the rollover if state is already ON
if (!matches) {
imgsrcON = imgsrc.replace(/.jpg$/ig,"-over.jpg"); // strip off extension
$(this).children("img").attr("src", imgsrcON);
}
});
$("#hoverlink a").mouseout(function(){
$(this).children("img").attr("src", imgsrc);
});
});
});
|

March 7th, 2013, 01:10 PM
|
 |
|
|
Join Date: Jan 2004
Location: New Springfield, OH
|
|
|
Clicking back in Chrome cause the page to be reloaded. Clicking back in other browsers loads the cached view.
One solution would be to write a function that capture mouse movements. If it's not over the images, they should be reset.
I'm sure you could also use a focus handler somewhere to do it is as well. Maybe on the body element. To be honest, I'm not sure what element has focus by default when a page is displayed. I've never had a need to know. It wouldn't be hard to figure out though. Just apply a global handler to every element that alerts what it is.
Realistically though, I wouldn't even worry about it. It's not that big of a deal. Never spend too much time coding around browser behavior. Your visitors probably won't even notice the difference.
Last edited by Nilpo : March 7th, 2013 at 01:13 PM.
|

March 7th, 2013, 01:19 PM
|
|
Contributing User
|
|
Join Date: Jun 2011
Posts: 33
Time spent in forums: 15 h 6 m 53 sec
Reputation Power: 2
|
|
|
Im still not good at jquery so I cant write you the code but would it not be possible to just simulate a mouseout right before it navigates to the new page?
in pseudo code something like:
soapsimage.clicked()
mouseout()
navigate(url = soaps.html)
*bad psuedocode I know lol*
But would that be possible?
|

March 7th, 2013, 01:33 PM
|
 |
|
|
Join Date: Jan 2004
Location: New Springfield, OH
|
|
Quote: | Originally Posted by harrjm Im still not good at jquery so I cant write you the code but would it not be possible to just simulate a mouseout right before it navigates to the new page?
in pseudo code something like:
soapsimage.clicked()
mouseout()
navigate(url = soaps.html)
*bad psuedocode I know lol*
But would that be possible? | Along those same lines, why not reset them in the click handler for $("#hoverlink a")?
|

March 7th, 2013, 05:30 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 40 m 24 sec
Reputation Power: 0
|
|
|
hey, all. thanks for the discussion. I wish I could run with these suggestions, but I didn't write the original script. It was a copy/paste from a tutorial on implementing these types of image rollovers. Hate to be so needy, but can anyone walk me through this? I would love to ignore it, because it doesn't really bother me, but it's my client that noticed and asked me to fix.
|

March 7th, 2013, 05:45 PM
|
 |
|
|
Join Date: Jan 2004
Location: New Springfield, OH
|
|
Quote: | Originally Posted by jorwesflow hey, all. thanks for the discussion. I wish I could run with these suggestions, but I didn't write the original script. It was a copy/paste from a tutorial on implementing these types of image rollovers. Hate to be so needy, but can anyone walk me through this? I would love to ignore it, because it doesn't really bother me, but it's my client that noticed and asked me to fix. | Educate your client.
That being said, I can help you as soon as I'm back at my desk. it's tough to write code on a tablet. 
|

March 7th, 2013, 06:07 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 40 m 24 sec
Reputation Power: 0
|
|
|
haha! sounds good
|

March 14th, 2013, 06:57 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 40 m 24 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by Nilpo Educate your client.
That being said, I can help you as soon as I'm back at my desk. it's tough to write code on a tablet.  |
Hey, Nilpo, I hate to pester, but did you come up with any ideas on this? Thanks in advance!
|

March 14th, 2013, 11:04 PM
|
 |
|
|
Join Date: Jan 2004
Location: New Springfield, OH
|
|
Sorry. Add this line after your jQuery code.
Code:
window.onunload = function(){};
As long as this line appears somewhere where it gets executed, it should fix the problem.
Code:
jQuery(function($) {
$(document).ready(function() {
// Preload all rollovers
$("#hoverlink img").each(function() {
// Set the original src
rollsrc = $(this).attr("src");
rollON = rollsrc.replace(/.jpg$/ig,"-over.jpg");
$("<img>").attr("src", rollON);
});
// Navigation rollovers
$("#hoverlink a").mouseover(function(){
imgsrc = $(this).children("img").attr("src");
matches = imgsrc.match(/-over/);
// don't do the rollover if state is already ON
if (!matches) {
imgsrcON = imgsrc.replace(/.jpg$/ig,"-over.jpg"); // strip off extension
$(this).children("img").attr("src", imgsrcON);
}
});
$("#hoverlink a").mouseout(function(){
$(this).children("img").attr("src", imgsrc);
});
});
});
window.onunload = function(){};
|

March 15th, 2013, 03:39 PM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 5
Time spent in forums: 40 m 24 sec
Reputation Power: 0
|
|
perfect! thank you so much! 
|

March 15th, 2013, 03:49 PM
|
 |
|
|
Join Date: Jan 2004
Location: New Springfield, OH
|
|
Quote: | Originally Posted by jorwesflow perfect! thank you so much!  | No problemo! I'm glad I could help.
I like the ones with nice simple fixes. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|