views:

51

answers:

1

Hi there

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

+2  A: 

You need to use the return keyword to return the new value, and assign the variable to the CurrentImage variable in the first function.

function IECurrentImage (CurrentImage)
{   
    var loc = document.location.href;
    CurrentImage = CurrentImage.replace(/\ /g, "%20");
    return loc.substring( 0, loc.lastIndexOf( '/' ) ) + '/' + CurrentImage;   
}

function ShowAltTag()
{
    var CurrentImage = $("#ShowImage img").attr("src");
    if ( $.browser.msie ) {
        CurrentImage = IECurrentImage(CurrentImage);
    } 

    // Etc.
}

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 CurrentImage statements 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.

Thorarin
that's it :) thank's to all for the help, and I apologize for the chaos I've made^^
ValiL