0

Here's my code, any idea why it's not working?

<script type="text/javascript">

windows.onload = function() {

function(){
    document.body.style.backgroundColor = ('red');
}
};


</script>
Simon Bergot
  • 9,481
  • 7
  • 35
  • 53
Lucenzo
  • 5
  • 1

2 Answers2

2

Without jquery, you can do that :

window.onload = function(){
    document.body.style.backgroundColor = 'red';
};

In your code you declared a function inside the callback you give to onload but you didn't call it.

Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
  • Nope. I know how to do it with jQuery. I want to do it WITHOUT it. Sorry for not being clear. – Lucenzo Sep 11 '12 at 15:17
  • Wow it works. Funny thing I've tried this. Must've overlooked something :D – Lucenzo Sep 11 '12 at 15:20
  • 1
    @Lucenzo you had `windowS.onload` instead of `window.onload` – mornaner Sep 11 '12 at 15:21
  • Look at the source code, around line 6555 in the current release. jQuery essentially does what you've written in your question but also does some checks to ensure cross browser compatibility which may be applicable to some styles and values. It also provides support for css hooks. So while the end result in this case is the same jQuery offers a lot more features, as you'd expect. – James Gaunt Sep 11 '12 at 15:21
  • `window.onload` and `$(document).ready` are NOT the same thing. [Reference](http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery) – jbabey Sep 11 '12 at 15:21
0

You're declaring an anonymous function but never executing it. Also, windows.onload is probably not what you meant.

What were you expecting to happen?

If you're trying to change the background color when the document is ready:

$(document).ready(function () {
    $('body').css('backgroundColor', 'red');
});​

http://jsfiddle.net/jbabey/8hQyu/

Simulating a document.ready callback without jQuery is not really a trivial task, see this question.

As far as the background color goes, this works fine:

document.body.style.backgroundColor = ('red');
Community
  • 1
  • 1
jbabey
  • 42,963
  • 11
  • 66
  • 94
  • I've explained in the comment above, I know how to do it with jQuery. I was trying to do it without jquery. – Lucenzo Sep 11 '12 at 15:23