tags:

views:

20

answers:

1

I'm trying to alert a message when the browser is resized or scrolled. I'm detecting the 2 events in the body

<body onResize="doDisp();" onScroll="doDisp();" >

where doDisp is this inside the <script> tag

<script type="text/javascript">

  function doDisp(){
    alert("browser changing state");
  }

</script>

but isn't it bad practice to have javascript in the body tag? Is there a cross-browser way to keep all the javascript inside the <script> tags?

A: 

You must write the script tag in the body because it requires that the Dom is loaded:

<script type="text/javascript">

      document.body.onresize=doDisp;
      document.body.onscroll=doDisp;
      function doDisp(){
        alert("browser changing state");
      }

    </script>
mck89
Can you clarify. Are you saying that if I want to use the `document.body.onresize` way, I'd have to put the whole `<script>` tag inside the `<body>` not the `<head>` as usual?
karl
Yes because if you put it in the head you must find a way to wait until the body element has been created, while if you put it in the body the script tag is executed after the body has been created so you can assign events to it.
mck89