I have 2 functions in my script:
function ShowAltTag(){
var CurrentImage = $("#ShowImage img").attr("src");
if( $.browser.msie ) {
IECurrentImage (CurrentImage);
}
if ($(".ImageRoller img[src='" +CurrentImage+ "']").attr("alt")){
var alt = $(".ImageRoller img[src='" +CurrentImage+ "']").attr("alt");
$("#ShowImage").append("<div class='alt'><span>" +alt+ "</span></div>");
$("#ShowImage div.alt").fadeIn("fast");
}
}
function IECurrentImage (CurrentImage)
{
var loc = document.location.href;
CurrentImage = CurrentImage.replace(/\ /g, "%20");
CurrentImage = loc.substring( 0, loc.lastIndexOf( '/' ) ) +'/'+ CurrentImage;
}
If the browser is IE it starts the function IECurrentImage , but after that the script should make the rest of ShowAltTag with the new value of Current Image.
Is that even possible?
Thanks in advance
You need to use the
returnkeyword to return the new value, and assign the variable to theCurrentImagevariable in the first function.Even though the variables in both functions have identical names, they are actually different variables, as each function has their own variable scope.
Alternatively, you can remove the existing
var CurrentImagestatements and put it outside and in front of both of these functions. This would make it a global variable. You shouldn’t use global variables unless it’s actually necessary though.You might want to read this article about local and global variables in JavaScript.