0

I have values in my function but it says it isn't defined.

This is my code:

<img onload="getFullnameDetails(263,225)" src="'+getBaseURL()+'js/check/no-image.png" rel="fullname" />

function getFullnameDetails(mainHeight,upperstyle){
setTimeout("fullnameCenter(mainHeight,upperstyle)",2000);
}

function fullnameCenter(mainHeight,upperstyle){
    var distOfMainAndUpper = 38;
    var mainHalfHeight = 131.5;
    var imageHeight = jQuery("img[rel='fullname']").height(); //there is a delay
    var imageHalfHeight = imageHeight/2;
    var fromImageTopToMainHalf = mainHalfHeight - imageHeight;
    var position = imageHalfHeight+fromImageTopToMainHalf-distOfMainAndUpper;
    jQuery(".test1").css("bottom",position+"px");
}

It says here that mainHeight is not defined. Why is this happening. This happens on this line: setTimeout("fullnameCenter(mainHeight,upperstyle)",2000);

Thanks in advance ;)

3 Answers 3

2

Try this

function getFullnameDetails(mainHeight,upperstyle){
    setTimeout(function() {fullnameCenter(mainHeight,upperstyle);},2000);
}
Sign up to request clarification or add additional context in comments.

Comments

1

When you set a timeout, it runs in the global scope so mainHeight and upperstyle are no longer in scope. You are better off using an anonymous function and providing the parameters:

setTimeout(function() {
    fullnameCenter(mainHeight,upperstyle);
}, 2000);

Comments

0
  var imageHeight = jQuery("img[rel='fullname']").height(); //there is a delay

you get the jquery image object and it not work! and let me tel you a secret you don't need the delay try this:

var imageHeight = jQuery("img[rel='fullname']")[0].height;

you see that i added the "[0]" after the jquery selector it will return the html image element and not the image jquery object.

Comments

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.