Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with simple rollover script 1

Status
Not open for further replies.

Foamcow

Programmer
Nov 14, 2002
6,092
GB
Hi,

I'm not big on Javascript, it's kind of on the periphary of what I do but I've had need to knock up a simple rollover script for a current job.
It doesn't need to be particularly portable but I would like to easily be able to add/remove buttons without messing with the javascript. Apologies in advance for any horrible coding faux pas I've made :)

Code:
	window.onload=function(){
		var nav = document.getElementById('nav');
		
		var buttons = nav.getElementsByTagName('IMG');

		for(var i=0; i<buttons.length; i++){
			buttons[i].onmouseover = function(){
				var original = this.getAttribute('src');
				var original = original.split('/');
				this.setAttribute('src','images/' + 'on_' + original[1]);
			}
			
			buttons[i].onmouseout = function(){
				var original = this.getAttribute('src');
				var original = original.split('_');
				this.setAttribute('src','images/btn_' + original[2]);
			}
		}
	
	}

Now to the problem...
The script is meant to swap out a few images by altering the src attribute. More specifically changing the src from say,

btn_about.png to on_btn_about.png

This seems to work fine in Firefox and Safari but doesn't work in IE (windows).

When I rollover one of the buttons, the src changes but it just changes to "on_" rather than "on_btn_about.png"


Any ideas why?

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
Try one of the following:

- When using setAttribute, add a second parameter, 1, to the call:

Code:
el.setAttribute('attr', 1);

- Don't use get/setAttribute, read the src property directly.

Failing that, test that the second array element ([1]] actually holds what you expect - it may well be that it doesn't).

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Cheers Dan,

that array element must have what i need in there or FF wouldn't work, right?

Anyway, I tested it, it looked right.

So I tried setting the src directly and it now works. yay!

Thing is, the onmouseout event still uses the DOM method and that works too! So I guess it must have been something in the way I was building the string for the new src?

Oh well, its working and its not worth spending any more time on!

Cheers

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
Despite saying I wouldn't spend more time on it I did...

I thought it might be the spaces in my original method.
So I took them out and IE worked.

Cool.

To confirm the test, I put the spaces back in, expecting it to break again.... IE still worked.

So now I have the same script that didn't work, but it works.

I'm going back to bed.

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
src may be retrieved in the form of absolute path In case of or file://..., component[1] may be therefore empty.
 
Yep the problem was that FF, Safari etc got src as say,

images/btn_about.png

Where as IE was getting the full absolute path.


I changed the script to this...

Code:
window.onload=function(){
		var nav = document.getElementById('nav');
		
		var buttons = nav.getElementsByTagName('IMG');

		for(var i=0; i<buttons.length; i++){
			buttons[i].onmouseover = function(){
				var path = this.getAttribute('src');
				path = path.split('/');
				var filename = path[path.length -1];
				this.setAttribute('src','images/' + 'on_' + filename);
			}
			
			buttons[i].onmouseout = function(){
				var original = this.getAttribute('src');
				alert(original);
				original = original.split('_');
				this.setAttribute('src','images/btn_' + original[2]);
			}
		}
	
	}


The mouseout function still works because I am splitting on the "_" rather than on "/". So it will work so long as there are no other "_" in the pathname

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
foamcow, if you want to take the suave approach here's another solution of extracting the filename:
Code:
<script type="text/javascript">

function sexyRegExpSolution(str) {
   var returnValue = /\/([^\/]+)$/.exec(str);
   if (returnValue) {
      //this found a match and will return the first parenthesized match
      return returnValue[1];
   }
   else {
      //this means no match was found and will return null
      return returnValue;
   }
}

var fileName = "[URL unfurl="true"]http://www.whatever.com/blah/images/this_is_my_test.jpg";[/URL]
alert(sexyRegExpSolution(fileName));


var fileName2 = "images/this_is_my_test.jpg";
alert(sexyRegExpSolution(fileName2));


var fileName3 = "this will not find a match so it will return null";
alert(sexyRegExpSolution(fileName3));

</script>

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
I wonder which category I fall into? [smile]

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Actually I considered using regexp or something and making the thing more portable so I could drop it into other sites.

Then I thought, "what's the point?"
I normally use CSS for rollovers and if I really need a fully portable JS rollover script I can just go grab one from a site made with Dreamweaver :)

Saying that, I much prefer to keep the HTML clean so it might be worth doing using "unobtrusive" methods. Certainly already been done before though.

<honk>*:O)</honk>
Foamcow Heavy Industries - Web site design in Cheltenham and Gloucester
Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
what makes a regex "sexy" ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
what makes a regex "sexy" ?

Hmm.... I'm not really sure. For some reason I always use the word "sexy" when I describe regexp......

I guess the best answer would be that it usually knocks multiple lines of code down to one line, and that is usually pretty sexy [wink]

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
I think you need to get our more kaht - lol

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I have a 4 1/2 month old son (my first), so getting ou[!]t[/!] isn't really an option [lol]

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
congrats and all the best for the future :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top