简体   繁体   English

jsf中的window.onload不起作用

[英]window.onload in jsf not working

I have to call a javascript function on click and onload of a checkbox. 我必须在单击和加载复选框时调用javascript函数。 In that javascript function, i have to pass a jsf param. 在该javascript函数中,我必须传递一个jsf参数。 the value of the jsf parameter is evaluated and passed on runtime. jsf参数的值将在运行时进行评估并传递。 Value of myClassDataItem.message is being evaluated correctly for Onclick function but in window.onload, value is not being evaluated. 正在为Onclick函数正确评估myClassDataItem.message的值,但在window.onload中,未评估值。 Is the syntax correct? 语法正确吗?

<h:selectBooleanCheckbox id="firstCheckbox" 
onclick= "showMsg('#{myClassDataItem.message}' );"/>

window.onload =  showMsg('#{myClassDataItem.message}');

Place the window.onload call inside a <script> -Block: window.onload调用放在<script> -Block内:

<script type="text/javascript">
  window.onload =  showMsg('#{myClassDataItem.message}');
</script>

and it works. 而且有效。 My echoController simply returns a string object: 我的echoController只是返回一个字符串对象:

@Named
@SessionScoped
public class EchoController implements Serializable {
  public String getEchoString() { return "Echo"; }
}

and the following pice of code works fine: 并且以下几段代码可以正常工作:

<script type="text/javascript">
  function showMsg(msg) { alert(msg); }
  window.onload = showMsg('#{echoController.echoString}');
</script>

But as mentioned by BalusC inside his answer here you should not do this at all. 但是正如BalusC在他的回答中提到的那样您根本不应该这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM