How to define the onLoad function in a JSF template to be defined elsewhere

I am developing a presentation for my site that has a standard login and landing page, and I want the onLoad function to call my login page, but not for my other pages (for now). I have a template.xhtml file that has this insert:

<div id="content"> <ui:insert name="content"/> </div>

Then in login.xhtml I have:

<ui:define name="content"> ... </ui:define>

Normally I would put this in login.xhtml:

<body onload="document.getElementById('login_form:name').focus();">

But since I use JSF ui composition tags, I cannot have the <body/> in login.xhtml (at least the way I'm trying to do this).

Is there any way to do this with the structure I described? The way I think is to make the onLoad function call the function in the template, and then each page with ui: define would fill that function. Is it possible?

Thanks!

+4
source share
1 answer

I can think in at least two ways:

  • define the header section using <ui:define name="header"> and place the javascript function bodyLoaded(){..} ( function bodyLoaded(){..} ) in it - differently on each page, and then refer to it via <body onload="bodyLoaded();">

  • use facelets options. That is, <body onload="#{onLoadJS}"/> and on each page, including the template, use <ui:param name="onLoadJS" value="document.getElementById(..)" />

+7
source

Source: https://habr.com/ru/post/1303062/


All Articles