JSF is in the context of this question merely a HTML/JS code generator. You don't actually pass anything from JSF to HTML/JS. You actually just let JSF in the webserver print the desired HTML/JS code so that the webbrowser can in turn properly execute it.
So, all you need to do is to just write JSF code in such way that it prints exactly the desired JS code.
<script>
var text = '<h:outputFormat value="Hello {0}"><f:param value="World" /></h:outputFormat>';
</script>
When opening the JSF page in browser (in other words, when sending a HTTP request to the server which invokes the FacesServlet
which in turn does all the JSF work of generating the HTML/JS), and doing a rightclick and View Source, you should see something like this:
<script>
var text = 'Hello World';
</script>
Your JS code which is executed hereafter should be able to grab this value by variable name text
.
Be however careful with special characters such as newlines. You may need to create a custom EL function which delegates to Apache Commons Lang StringEscapeUtils#escapeJavaScript()
, see also How to escape JavaScript in JSP?
Update: as per the comments you seem to be using JSF 1.1. If upgrading is not an option (I however strongly recommend to do so; practically any JSF 1.1 project is without code changes forwards compatible with JSF 1.2), then you need to put the non-JSF markup in <f:verbatim>
"the usual way".
<f:verbatim><script>
var text = '</f:verbatim><h:outputFormat value="Hello {0}"><f:param value="World" /></h:outputFormat><f:verbatim>';
</script></f:verbatim>
See also among others Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards? and JSF/Facelets: why is it not a good idea to mix JSF/Facelets with HTML tags? for some background information.
As per your complaint,
And an additional drawback is that I cannot put that in the <head>
since it has to be inside <f:view>
.
this is absolutely not true. Definitely you can put this in <head>
. Just move the <f:view>
from inside <body>
to outside the <body>
, wrapping both <head>
and <body>
or even <html>
.
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<!DOCTYPE html>
<html lang="en">
<f:view>
<head>...</head>
<body>...</body>
</f:view>
</html>