5

I am using sitemesh in our application. In decorator jsp I have added <decorator:head> in head and on body tag:

<body onload="<decorator:getProperty property='body.onload'/>" >

So I want to handle body onload on my jsp page. I have added following things:

<script type="text/javascript">
    function init() {
        alert("hi");
    }
</script>
</head>
<body onload="javascript:init();">

But init() does not worked in my jsp page.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Raje
  • 3,285
  • 15
  • 50
  • 70
  • Don't use the `javascript:` pseudo-protocol in inline event handler attributes; you're merely adding a useless JavaScript label there. Moreover, this should work as expected, what exactly “does not work”? – Marcel Korpel Jun 13 '11 at 11:04
  • @Narcel: On body onload i want perform some layout issue for different resolution. one div consist of jquery component when we specify height in percentage it does not work. so for this on body onload i m specifying height in Pixel. but body onload does not work on jsp page. – Raje Jun 14 '11 at 04:15

2 Answers2

8

Why not just stick it all into the script element? Much cleaner than mucking about with element attributes:

window.onload = function() {
    alert('hi');
};

Or, alternatively, keeping the init declaration:

window.onload = init;
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • Thanks for reply. On body onload event i want to handle some layout issue. on window.onload event some flickering issue is occur. – Raje Jun 13 '11 at 10:49
  • @amit: the `onload` attribute of the `body` element is the same as `window.onload` – Marcel Korpel Jun 13 '11 at 11:04
  • you might wish to use `document.onreadystatechange=init;` but it is a bit more complex -> http://msdn.microsoft.com/en-us/library/ms536957(v=vs.85).aspx the difference is that *window.onload* fires when all the linked elements in the document are loaded while *document.onreadystatechange* fires when the html part is loaded and do not wait for the images – venimus Jun 13 '11 at 13:39
  • @Venimus: thanks for reply. but where should i put document.onreadystatechange=init in my jsp page. means whether in head tag or outside head tag? – Raje Jun 14 '11 at 04:18
2

try this

<script type="text/javascript">
    function init() {
        alert("hi");
    }
</script>
</head>
<body onload="init();">
beta
  • 268
  • 1
  • 8