0

I have a javascript function that dynamically positions a background image:

  window.onload = function() {
    bw = document.getElementsByClassName("BottomWrapper");
    if (bw.length===0){
      bw = document.getElementsByClassName("BottomWrapper2");
    }
    pos = window.innerWidth/2 - 490.5;
    bw=Array.prototype.slice.call(bw);
    bw[0].style.backgroundPosition="center 0px, " + pos + "px 0px";
  };

and it works. But if I name the same exact function and run it:

function position() {
  bw = document.getElementsByClassName("BottomWrapper");
  if (bw.length===0){
    bw = document.getElementsByClassName("BottomWrapper2");
  }
  pos = window.innerWidth/2 - 490.5;
  bw=Array.prototype.slice.call(bw);
  bw[0].style.backgroundPosition="center 0px, " + pos + "px 0px";
};
position();

I get the error:

TypeError: Cannot read property 'style' of undefined

I'm mystified. Why does the first function work and not the second?

5
  • Are you asking what window.onload does? Commented Dec 10, 2012 at 0:45
  • window.onload = position(); Commented Dec 10, 2012 at 0:45
  • @SReject: Almost. Remove the (). Commented Dec 10, 2012 at 0:50
  • @Craig Simplify the problem by removing the "bad" function and only using the "good" function by name; e.g. how does window.onload = position compare with position(). Extracting the definition of position eliminates the duplication and a potential source of "differences" for an "identical" function. Once this is done it can be safely argued for that the execution context is what matters or if something else is amiss. (Also, I recommend using local variables to avoid contamination.) Commented Dec 10, 2012 at 0:58
  • Oops. I did mean, without the ()'s Commented Dec 10, 2012 at 13:12

2 Answers 2

3

if bw[0] is undefined, that means that document.getElementsByClassName("BottomWrapper2") returned undefined, which most likely means that the div was not on the page when the script was ran. If you load your code after your tag, this shouldn't happen.

The reason the first would work is because it is waiting for the onload event to fire.

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

3 Comments

well I've run the 'position()' function from the javascript console after the page was loaded and I get the same result. The function also works when I attach it to the window.resize event. Also, I have the code in the footer below the div in question...so I don't see that this could be the issue.
@Craig Simplify the problem by removing the "bad" function and only using the "good" function by name; e.g. how does window.onload = position compare with position(). Extracting the definition of position eliminates the duplication and a potential source of "differences" for an "identical" function. Once this is done it can be safely argued for that the execution context is what matters or if something else is amiss. (Also, I recommend using local variables to avoid contamination.)
my bad... I had the code in the footer but it turns out I also had the same code in a separate file that was included in the head ... sorry for the false alarm and thanks for all your responses!
0

If you use the function like this, it will work

<html><head>
<script>

function position() {
  bw = document.getElementsByClassName("BottomWrapper");
  console.log(bw);
  if (bw.length===0){
    bw = document.getElementsByClassName("BottomWrapper2");
  }
  pos = window.innerWidth/2 - 490.5;
  bw=Array.prototype.slice.call(bw);
  bw[0].style.backgroundPosition="center 0px, " + pos + "px 0px";
  console.log(bw[0]);
};
   </script>
<body onload="position()">
<div class="BottomWrapper2"></div>
</body></head></html>

The previous ones causes error because, the function is called before the body is loaded

1 Comment

my bad... I had the code in the footer but it turns out I also had the same code in a separate file that was included in the head ... sorry for the false alarm and thanks for all your responses!

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.