can I update the title bar?

后端 未结 1 1068
无人共我
无人共我 2021-01-25 15:24

In a previous question I asked about updating a menu bar. BalusC told me that I need to add the form containing the menu bar.

I would like to extend that question to ask

相关标签:
1条回答
  • 2021-01-25 15:34

    You can't update the title by JSF ajax update simply because <title> is not a JSF component. You also can't put HTML or JSF components in <title>, this is illegal HTML syntax.

    Your best bet is to use JavaScript instead to update the title by assigning it to the document.title. You can use RequestContext#execute() for this.

    String fullName = current.getFirstName() + " " + current.getSurName();
    context.execute("document.title='" + fullName + "'");
    

    Since this seems to be user-controlled data, I would use StringEscapeUtils#escapeJavaScript() to escape it in order to prevent potential XSS attack holes.

    String fullName = current.getFirstName() + " " + current.getSurName();
    context.execute("document.title='" + StringEscapeUtils.escapeJavaScript(fullName) + "'");
    

    An alternative is to use OmniFaces <o:onloadScript>.

    <o:onloadScript>document.title='#{of:escapeJS(viewBacking.current.firstName)} #{of:escapeJS(viewBacking.current.surName)}'</o:onloadScript>
    

    This will be re-executed on every ajax request.

    0 讨论(0)
提交回复
热议问题