Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle body onload event in <head> tag

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.

like image 352
Raje Avatar asked Jun 13 '11 10:06

Raje


People also ask

What does <body onload=””> do in HTML?

<body onLoad=””>. In HTML Attributes. 0 Shares. The onLoad attribute is an event handler that instructs the browser to run a script when the visitor loads the page. This is commonly used to forward the visitor to another page, but can be used to trigger pop-up boxes, or execute a script based on the user's browser version.

How to handle the page load event in JavaScript?

To handle the load event, you register an event listener using the addEventListener () method: window.addEventListener ('load', (event) => { console.log ('The page has fully loaded'); }); Code language: JavaScript (javascript) Or using the onload property of the window object:

How do I add an onload event handler to an HTML document?

Or using the onload property of the window object: If you maintain a legacy system, you may find that the load event handler is registered in of the body element of the HTML document, like this: It’s a good practice to use the addEventListener () method to assign the onload event handler whenever possible.

What is the use of body event in HTML?

This is commonly used to forward the visitor to another page, but can be used to trigger pop-up boxes, or execute a script based on the user’s browser version. body event handlers act on the browser window, and the script will only be executed once the page is completely finished loading.


1 Answers

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;
like image 162
lonesomeday Avatar answered Oct 03 '22 09:10

lonesomeday