1

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!

1
  • You probably want console.log(getURL('light_bulb.png')); Commented Jul 19, 2013 at 16:12

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and it didn't seem to work in my testing. Guess I'll try again; I think the frames are making it extra difficult.
1

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.

1 Comment

Thanks, I knew I had to call the function somewhere, just couldn't figure out where! This seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.