最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to handle body onload event in <head> tag - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jul 31, 2020 at 21:20 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 13, 2011 at 10:26 RajeRaje 3,33315 gold badges54 silver badges70 bronze badges 2
  • 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 Commented Jun 13, 2011 at 11:04
  • @Narcel: On body onload i want perform some layout issue for different resolution. one div consist of jquery ponent 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 Commented Jun 14, 2011 at 4:15
Add a ment  | 

2 Answers 2

Reset to default 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;

try this

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

评论列表(0)

  1. 暂无评论