1

I am trying to use this script to run a function on the page load. Can anyone see what's wrong? Stupid error, I'm sure - just can't work it out.

Code:

<script>

  var text = document.getElementById("myText");

  function changeFontSize(){
  if (window.innerWidth < 600) {
text.style.fontSize = "70px";
      } else {
      text.style.fontSize = "100px";
      }
  }

  window.onresize = changeFontSize;

  </script>

  <script>
  setTimeout(function() changeFontSize, 3000);
  </script>
1
  • Try to use window.onload = changeFontSize; Commented Jul 13, 2015 at 17:21

5 Answers 5

2
window.onload = changeFontSize;
window.onresize = changeFontSize;

alternatively, you can use

window.addEventListener("onload", changeFontSize, false);
window.addEventListener("onresize", changeFontSize, false);
Sign up to request clarification or add additional context in comments.

5 Comments

tried that but it didn't work - i want the javascript to run once when the window is loaded and then with the listener for whenever it is resized.
ok it's working now :) but it is quite slow, there's a good one-two second wait until it runs it. Any ideas?
yes, that is because onresize is demanding too much cpu power. See this stackoverflow.com/questions/8040092/…
really? I have an 8 core cpu so that shouldn't be a problem.
also, I am using this code: pastebin.com/GJi0KZyS but it isn't working on resize any more...
0

  var text = document.getElementById("myText");

  function changeFontSize() {
    if (window.innerWidth < 600) {
      text.style.fontSize = "70px";
    } else {
      text.style.fontSize = "100px";
    }
  }

  window.onresize = changeFontSize;

  setTimeout(changeFontSize, 3000);
<p id="myText">Test</p>

Basically you had a syntax error in:

setTimeout(function()  changeFontSize, 3000);

You don't need the function()

1 Comment

unfortunately this didn't work either...I would like to run the function once when the page is loaded and then whenever it is resized.
0

To start with, setTimeout() isn't used the way you wrote it, it should be setTimeout(changeFontSize, 3000); instead. That being said this would work:

<script>
    var text = document.getElementById("myText");

    function changeFontSize() {
        if (window.innerWidth < 600) {
            text.style.fontSize = "70px";
        } else {
            text.style.fontSize = "100px";
        }
    }

    window.onresize = changeFontSize;

    changeFontSize();
</script>

As was also said by jperelli, window.onload = changeFontSize would also work.

2 Comments

thanks but it didn't work. the javascript should run once on the load of page/timeout and then whenever windows is resized.
@dwinnbrown Are you putting the <script> block in the <head> tags or at the end of the body (after the elements you want to re-size)? What's the behavior that you're seeing?
0

If you want absolutely use onresize instead onload (I suppose that you want to achieve something responsive), you need to wait the page is loaded, otherwise the object myText will not the object will not yet been initialized in the variable text ('cause it's not yet available in the DOM). Note that in this case the use of a setTimeout (you wrong the syntax anyway) is not necessary.

This works:

<script>

  function changeFontSize(){
      if (window.innerWidth < 600) {
          document.getElementById("myText").style.fontSize = "70px";
      } else {
          document.getElementById("myText").style.fontSize = "100px";
      }
  }

  window.onresize = changeFontSize;

</script>

1 Comment

thanks but it didn't work. the javascript should run once on the load of page/timeout and then whenever windows is resized.
0

  var text = document.getElementById("myText");

  function changeFontSize(){
  if (window.innerWidth < 600) {
text.style.fontSize = "70px";
      } else {
      text.style.fontSize = "100px";
      }
  }

  window.onload = changeFontSize;
  window.onresize = changeFontSize;
<div id="myText">Test</div>

1 Comment

thanks but it didn't work. the javascript should run once on the load of page/timeout and then whenever windows is resized.

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.