ham on wry
ham on wry

Reputation: 71

Accessing value of function in multiple functions

tl;dr: How do I reuse the first function in other functions? It keeps returning undefined if called in other functions.

I create online help (I'm not a programmer), which unfortunately is output in framesets by Adobe RoboHelp. I would like to use the first function below (getURL) to dynamically build a URL that can be reused in other functions. For example, passing the "a" argument as a graphic in one function, or using it to send the page in the frameset as a mailto: link in another function.

The problem I'm having is that calling the getURL function from within other functions; the fullURL value is being returned as undefined.

function getURL(a) {
    var frameURL = window.frames[1].frames[1].document.location, 
    frameareaname = frameURL.pathname.split('/').slice(4, 5), 
    frameprojname = frameURL.pathname.split('/').slice(6, 7),
    protocol_name = window.location.protocol,
    server_name = window.location.host,
fullURL = protocol_name + '//' + server_name + '/robohelp/robo/server/' + frameareaname + '/projects/' + frameprojname + '/' + a;
return fullURL;
}

If I call the function like this, it works fine, but not if I place it inside a function:

 getURL('light_bulb.png');
 console.log(fullURL);

How can I reuse this function in another function? For example, fullURL should be the background image:

  $('.Graphic, .GraphicIndent, .Graphic3rd, .Graphic4th').not('.Graphic-norollover').mouseover(function()
  {
    var imgWidth = $(this).children('img').width();
    $(this).css('background', 'url(' + fullURL + ') 50% 50% no-repeat #000');
    $(this).css('width', imgWidth);
    $(this).children('img').fadeTo(750, '.4');
    $(this).children('img').attr('alt', 'Click to view full-size graphic');
    $(this).children('img').attr('title', 'Click to view full-size graphic');
  });

Thanks!

Upvotes: 1

Views: 49

Answers (2)

tymeJV
tymeJV

Reputation: 104775

fullURL is what is returned from getURL, so call that function when you need the values:

var imageURL = getURL('some_image.png');

fullURL doesn't exist outside of getURL.

Upvotes: 3

David Sherret
David Sherret

Reputation: 106650

You have to call the function in order to be able to use it:

  $('.Graphic, .GraphicIndent, .Graphic3rd, .Graphic4th').not('.Graphic-norollover').mouseover(function()
  {
    var imgWidth = $(this).children('img').width();
    $(this).css('background', 'url(' + getURL('light_bulb.png') + ') 50% 50% no-repeat #000');
    $(this).css('width', imgWidth);
    $(this).children('img').fadeTo(750, '.4');
    $(this).children('img').attr('alt', 'Click to view full-size graphic');
    $(this).children('img').attr('title', 'Click to view full-size graphic');
  });

The fullURL is limited to the scope of the getURL function. It's not visible anywhere else. You have to call getURL to get the result of the function.

Upvotes: 1

Related Questions