You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Warren Bell <wa...@clarksnutrition.com> on 2005/11/10 03:19:23 UTC

JavaScript alert() window

I want to create a JavaScript alert window with a message from a resource
bundle when I have a message to display. Getting the message is no problem,
but getting it into the JavaScript is.

<script language="JavaScript" type="text/JavaScript">
function displayMessage()
{
  var message = "MESSAGE HERE";
  if(message == "")
  {
  }
  else
  {
    alert(message);
  }
}
</script>

<body onload="displayMessage()">


I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
#{mainMenu.alertMessage} would be an empty string or have a message. I am
getting var message = "#{mainMenu.alertMessage}" instead of the value of
#{mainMenu.alertMessage}.

Is there a way using MyFaces to achieve this?

Thanks,

Warren Bell


Re: JavaScript alert() window

Posted by Martin Marinschek <ma...@gmail.com>.
In JSF1.2, the newly specified "unified EL" will be supported anywhere
in the page.

This means that you will be able to keep on using JSP style EL
statements - those will be immediately executed on building the page,
and the JSF style EL statements (in the unified EL called "deferred"
EL statements) will be passed on to the JSF subsystem.

regards,

Martin

On 11/10/05, Mike Kienenberger <mk...@gmail.com> wrote:
> I'm pretty sure that Simon is correct about JSF EL working anywhere in
> JSF 1.2.   I know that facelets already supports using JSF EL anywhere
> in a page, even in comments.
>
> On 11/9/05, Simon Kitching <sk...@obsidium.com> wrote:
> > Warren Bell wrote:
> > > I want to create a JavaScript alert window with a message from a resource
> > > bundle when I have a message to display. Getting the message is no problem,
> > > but getting it into the JavaScript is.
> > >
> > > <script language="JavaScript" type="text/JavaScript">
> > > function displayMessage()
> > > {
> > >   var message = "MESSAGE HERE";
> > >   if(message == "")
> > >   {
> > >   }
> > >   else
> > >   {
> > >     alert(message);
> > >   }
> > > }
> > > </script>
> > >
> > > <body onload="displayMessage()">
> > >
> > >
> > > I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
> > > #{mainMenu.alertMessage} would be an empty string or have a message. I am
> > > getting var message = "#{mainMenu.alertMessage}" instead of the value of
> > > #{mainMenu.alertMessage}.
> > >
> > > Is there a way using MyFaces to achieve this?
> >
> > I think in the next release of JSP this will be possible (EL expressions
> > evaluated in the body of the JSP page). However that's no consolation
> > for you now :-) [and I might be wrong anyhow...]
> >
> > Right now, the only place an EL expression #{..} is evaluated is in the
> > attribute of a JSF tag, so your approach above won't work.
> >
> > You might be able to do something like this:
> >    <t:div style="display:none" id="mymessage" forceid="true">
> >      <h:outputText value="#{...}"/>
> >    </t:div>
> > to force some text to be output in a named div, but not visible to the
> > user. Then in your javascript you could do:
> >    var message = document.getElementById("mymessage").innerHtml();
> >    alert(message);
> >
> > The alternative would be to write a component that can emit arbitrary
> > text as a script variable, eg
> >    <t:var name="message" value="#{...}"/>
> > which generates something like:
> >    <script>var message="..."</script>
> >
> > There's the t:stylesheet tag which is sort of in this spirit.
> >
> > Note that writing a custom JSP component is sadly much more complicated
> > than it should be :-(
> >
> > Regards,
> >
> > Simon
> >
>


--

http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces

Re: JavaScript alert() window

Posted by Mike Kienenberger <mk...@gmail.com>.
I'm pretty sure that Simon is correct about JSF EL working anywhere in
JSF 1.2.   I know that facelets already supports using JSF EL anywhere
in a page, even in comments.

On 11/9/05, Simon Kitching <sk...@obsidium.com> wrote:
> Warren Bell wrote:
> > I want to create a JavaScript alert window with a message from a resource
> > bundle when I have a message to display. Getting the message is no problem,
> > but getting it into the JavaScript is.
> >
> > <script language="JavaScript" type="text/JavaScript">
> > function displayMessage()
> > {
> >   var message = "MESSAGE HERE";
> >   if(message == "")
> >   {
> >   }
> >   else
> >   {
> >     alert(message);
> >   }
> > }
> > </script>
> >
> > <body onload="displayMessage()">
> >
> >
> > I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
> > #{mainMenu.alertMessage} would be an empty string or have a message. I am
> > getting var message = "#{mainMenu.alertMessage}" instead of the value of
> > #{mainMenu.alertMessage}.
> >
> > Is there a way using MyFaces to achieve this?
>
> I think in the next release of JSP this will be possible (EL expressions
> evaluated in the body of the JSP page). However that's no consolation
> for you now :-) [and I might be wrong anyhow...]
>
> Right now, the only place an EL expression #{..} is evaluated is in the
> attribute of a JSF tag, so your approach above won't work.
>
> You might be able to do something like this:
>    <t:div style="display:none" id="mymessage" forceid="true">
>      <h:outputText value="#{...}"/>
>    </t:div>
> to force some text to be output in a named div, but not visible to the
> user. Then in your javascript you could do:
>    var message = document.getElementById("mymessage").innerHtml();
>    alert(message);
>
> The alternative would be to write a component that can emit arbitrary
> text as a script variable, eg
>    <t:var name="message" value="#{...}"/>
> which generates something like:
>    <script>var message="..."</script>
>
> There's the t:stylesheet tag which is sort of in this spirit.
>
> Note that writing a custom JSP component is sadly much more complicated
> than it should be :-(
>
> Regards,
>
> Simon
>

Re: JavaScript alert() window

Posted by Simon Kitching <sk...@obsidium.com>.
Yee CN wrote:
> I think the parsing rules must be:
> - JSF EL binding expressions only works within jsf tags
> - jsf tags evaluation take precedence over jsp and html tags
> 
> I just recalled that JSP EL works with JSF managed beans. The following
> should work. Take note that it is $, not #.
> 
> <body onload="displayMessage('${mainMenu.alertMessage}')">
> 

This will work when the mainMenu bean already exists, because JSF stores 
any managed bean objects into the request/session/app scope. The JSP EL 
looks in page/request/session/app for objects so will find it.

However if this is the first reference to the mainMenu object, then 
using #{...} syntax will cause the object to be created, while ${...} 
syntax will just fail as JSP doesn't know about managed beans.

Regards,

Simon

RE: JavaScript alert() window

Posted by Yee CN <ye...@streamyx.com>.
I think the parsing rules must be:
- JSF EL binding expressions only works within jsf tags
- jsf tags evaluation take precedence over jsp and html tags

I just recalled that JSP EL works with JSF managed beans. The following
should work. Take note that it is $, not #.

<body onload="displayMessage('${mainMenu.alertMessage}')">

-----Original Message-----
From: Warren Bell [mailto:warren@clarksnutrition.com] 
Sent: Thursday, 10 November 2005 2:27 PM
To: MyFaces Discussion; yeecn@streamyx.com
Subject: RE: JavaScript alert() window

It looks like <h:outputText value="#{mainMenu.alertMessage}"/> will be
evaluated and inserted anywhere on the page as long as it is not within
another tag. You would have issues of what gets parsed first if it was
within another tag.

Example:

<h:outputText value="<h:outputText value="<h:outputText value="<h:outputText
value="#{mainMenu.alertMessage}"/>"/>"/>"/>

> -----Original Message-----
> From: Yee CN [mailto:yeecn@streamyx.com]
> Sent: Wednesday, November 09, 2005 9:52 PM
> To: 'MyFaces Discussion'
> Subject: RE: JavaScript alert() window
>
>
> Ah - I see. <body> is not a jsf tag. But then why don't you get literal
> "<h:outputText value="#{mainMenu.alertMessage}"/>"?
>
> I think how .jsp file is being parsed is not clear at all.
>
> Anyway it is good to know that it works.
>
> Yee
>
> -----Original Message-----
> From: Warren Bell [mailto:warren@clarksnutrition.com]
> Sent: Thursday, 10 November 2005 1:06 PM
> To: MyFaces Discussion
> Subject: RE: JavaScript alert() window
>
> I was able to get it to work with:
>
> <body onload="displayMessage('<h:outputText
> value="#{mainMenu.alertMessage}"/>')">
>
> This evaluated correctly and gave me an alert window with the message.
>
> <body onload="displayMessage('#{mainMenu.alertMessage}')">
>
> This gave me an alert window with the literal message
> #{mainMenu.alertMessage}
>
> > -----Original Message-----
> > From: Simon Kitching [mailto:skitching@obsidium.com]
> > Sent: Wednesday, November 09, 2005 7:53 PM
> > To: MyFaces Discussion
> > Subject: Re: JavaScript alert() window
> >
> >
> > Warren Bell wrote:
> > > I want to create a JavaScript alert window with a message from
> > a resource
> > > bundle when I have a message to display. Getting the message is
> > no problem,
> > > but getting it into the JavaScript is.
> > >
> > > <script language="JavaScript" type="text/JavaScript">
> > > function displayMessage()
> > > {
> > >   var message = "MESSAGE HERE";
> > >   if(message == "")
> > >   {
> > >   }
> > >   else
> > >   {
> > >     alert(message);
> > >   }
> > > }
> > > </script>
> > >
> > > <body onload="displayMessage()">
> > >
> > >
> > > I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
> > > #{mainMenu.alertMessage} would be an empty string or have a
> > message. I am
> > > getting var message = "#{mainMenu.alertMessage}" instead of
> the value of
> > > #{mainMenu.alertMessage}.
> > >
> > > Is there a way using MyFaces to achieve this?
> >
> > I think in the next release of JSP this will be possible (EL expressions
> > evaluated in the body of the JSP page). However that's no consolation
> > for you now :-) [and I might be wrong anyhow...]
> >
> > Right now, the only place an EL expression #{..} is evaluated is in the
> > attribute of a JSF tag, so your approach above won't work.
> >
> > You might be able to do something like this:
> >    <t:div style="display:none" id="mymessage" forceid="true">
> >      <h:outputText value="#{...}"/>
> >    </t:div>
> > to force some text to be output in a named div, but not visible to the
> > user. Then in your javascript you could do:
> >    var message = document.getElementById("mymessage").innerHtml();
> >    alert(message);
> >
> > The alternative would be to write a component that can emit arbitrary
> > text as a script variable, eg
> >    <t:var name="message" value="#{...}"/>
> > which generates something like:
> >    <script>var message="..."</script>
> >
> > There's the t:stylesheet tag which is sort of in this spirit.
> >
> > Note that writing a custom JSP component is sadly much more complicated
> > than it should be :-(
> >
> > Regards,
> >
> > Simon
> >
> > __________ NOD32 1.1282 (20051109) Information __________
> >
> > This message was checked by NOD32 antivirus system.
> > http://www.eset.com
> >
> >
>
> __________ NOD32 1.1282 (20051109) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>


RE: JavaScript alert() window

Posted by Warren Bell <wa...@clarksnutrition.com>.
It looks like <h:outputText value="#{mainMenu.alertMessage}"/> will be
evaluated and inserted anywhere on the page as long as it is not within
another tag. You would have issues of what gets parsed first if it was
within another tag.

Example:

<h:outputText value="<h:outputText value="<h:outputText value="<h:outputText
value="#{mainMenu.alertMessage}"/>"/>"/>"/>

> -----Original Message-----
> From: Yee CN [mailto:yeecn@streamyx.com]
> Sent: Wednesday, November 09, 2005 9:52 PM
> To: 'MyFaces Discussion'
> Subject: RE: JavaScript alert() window
>
>
> Ah - I see. <body> is not a jsf tag. But then why don't you get literal
> "<h:outputText value="#{mainMenu.alertMessage}"/>"?
>
> I think how .jsp file is being parsed is not clear at all.
>
> Anyway it is good to know that it works.
>
> Yee
>
> -----Original Message-----
> From: Warren Bell [mailto:warren@clarksnutrition.com]
> Sent: Thursday, 10 November 2005 1:06 PM
> To: MyFaces Discussion
> Subject: RE: JavaScript alert() window
>
> I was able to get it to work with:
>
> <body onload="displayMessage('<h:outputText
> value="#{mainMenu.alertMessage}"/>')">
>
> This evaluated correctly and gave me an alert window with the message.
>
> <body onload="displayMessage('#{mainMenu.alertMessage}')">
>
> This gave me an alert window with the literal message
> #{mainMenu.alertMessage}
>
> > -----Original Message-----
> > From: Simon Kitching [mailto:skitching@obsidium.com]
> > Sent: Wednesday, November 09, 2005 7:53 PM
> > To: MyFaces Discussion
> > Subject: Re: JavaScript alert() window
> >
> >
> > Warren Bell wrote:
> > > I want to create a JavaScript alert window with a message from
> > a resource
> > > bundle when I have a message to display. Getting the message is
> > no problem,
> > > but getting it into the JavaScript is.
> > >
> > > <script language="JavaScript" type="text/JavaScript">
> > > function displayMessage()
> > > {
> > >   var message = "MESSAGE HERE";
> > >   if(message == "")
> > >   {
> > >   }
> > >   else
> > >   {
> > >     alert(message);
> > >   }
> > > }
> > > </script>
> > >
> > > <body onload="displayMessage()">
> > >
> > >
> > > I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
> > > #{mainMenu.alertMessage} would be an empty string or have a
> > message. I am
> > > getting var message = "#{mainMenu.alertMessage}" instead of
> the value of
> > > #{mainMenu.alertMessage}.
> > >
> > > Is there a way using MyFaces to achieve this?
> >
> > I think in the next release of JSP this will be possible (EL expressions
> > evaluated in the body of the JSP page). However that's no consolation
> > for you now :-) [and I might be wrong anyhow...]
> >
> > Right now, the only place an EL expression #{..} is evaluated is in the
> > attribute of a JSF tag, so your approach above won't work.
> >
> > You might be able to do something like this:
> >    <t:div style="display:none" id="mymessage" forceid="true">
> >      <h:outputText value="#{...}"/>
> >    </t:div>
> > to force some text to be output in a named div, but not visible to the
> > user. Then in your javascript you could do:
> >    var message = document.getElementById("mymessage").innerHtml();
> >    alert(message);
> >
> > The alternative would be to write a component that can emit arbitrary
> > text as a script variable, eg
> >    <t:var name="message" value="#{...}"/>
> > which generates something like:
> >    <script>var message="..."</script>
> >
> > There's the t:stylesheet tag which is sort of in this spirit.
> >
> > Note that writing a custom JSP component is sadly much more complicated
> > than it should be :-(
> >
> > Regards,
> >
> > Simon
> >
> > __________ NOD32 1.1282 (20051109) Information __________
> >
> > This message was checked by NOD32 antivirus system.
> > http://www.eset.com
> >
> >
>
> __________ NOD32 1.1282 (20051109) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>


RE: JavaScript alert() window

Posted by Yee CN <ye...@streamyx.com>.
Ah - I see. <body> is not a jsf tag. But then why don't you get literal
"<h:outputText value="#{mainMenu.alertMessage}"/>"?

I think how .jsp file is being parsed is not clear at all. 

Anyway it is good to know that it works.

Yee

-----Original Message-----
From: Warren Bell [mailto:warren@clarksnutrition.com] 
Sent: Thursday, 10 November 2005 1:06 PM
To: MyFaces Discussion
Subject: RE: JavaScript alert() window

I was able to get it to work with:

<body onload="displayMessage('<h:outputText
value="#{mainMenu.alertMessage}"/>')">

This evaluated correctly and gave me an alert window with the message.

<body onload="displayMessage('#{mainMenu.alertMessage}')">

This gave me an alert window with the literal message
#{mainMenu.alertMessage}

> -----Original Message-----
> From: Simon Kitching [mailto:skitching@obsidium.com]
> Sent: Wednesday, November 09, 2005 7:53 PM
> To: MyFaces Discussion
> Subject: Re: JavaScript alert() window
>
>
> Warren Bell wrote:
> > I want to create a JavaScript alert window with a message from
> a resource
> > bundle when I have a message to display. Getting the message is
> no problem,
> > but getting it into the JavaScript is.
> >
> > <script language="JavaScript" type="text/JavaScript">
> > function displayMessage()
> > {
> >   var message = "MESSAGE HERE";
> >   if(message == "")
> >   {
> >   }
> >   else
> >   {
> >     alert(message);
> >   }
> > }
> > </script>
> >
> > <body onload="displayMessage()">
> >
> >
> > I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
> > #{mainMenu.alertMessage} would be an empty string or have a
> message. I am
> > getting var message = "#{mainMenu.alertMessage}" instead of the value of
> > #{mainMenu.alertMessage}.
> >
> > Is there a way using MyFaces to achieve this?
>
> I think in the next release of JSP this will be possible (EL expressions
> evaluated in the body of the JSP page). However that's no consolation
> for you now :-) [and I might be wrong anyhow...]
>
> Right now, the only place an EL expression #{..} is evaluated is in the
> attribute of a JSF tag, so your approach above won't work.
>
> You might be able to do something like this:
>    <t:div style="display:none" id="mymessage" forceid="true">
>      <h:outputText value="#{...}"/>
>    </t:div>
> to force some text to be output in a named div, but not visible to the
> user. Then in your javascript you could do:
>    var message = document.getElementById("mymessage").innerHtml();
>    alert(message);
>
> The alternative would be to write a component that can emit arbitrary
> text as a script variable, eg
>    <t:var name="message" value="#{...}"/>
> which generates something like:
>    <script>var message="..."</script>
>
> There's the t:stylesheet tag which is sort of in this spirit.
>
> Note that writing a custom JSP component is sadly much more complicated
> than it should be :-(
>
> Regards,
>
> Simon
>
> __________ NOD32 1.1282 (20051109) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>


RE: JavaScript alert() window

Posted by Warren Bell <wa...@clarksnutrition.com>.
I was able to get it to work with:

<body onload="displayMessage('<h:outputText
value="#{mainMenu.alertMessage}"/>')">

This evaluated correctly and gave me an alert window with the message.

<body onload="displayMessage('#{mainMenu.alertMessage}')">

This gave me an alert window with the literal message
#{mainMenu.alertMessage}

> -----Original Message-----
> From: Simon Kitching [mailto:skitching@obsidium.com]
> Sent: Wednesday, November 09, 2005 7:53 PM
> To: MyFaces Discussion
> Subject: Re: JavaScript alert() window
>
>
> Warren Bell wrote:
> > I want to create a JavaScript alert window with a message from
> a resource
> > bundle when I have a message to display. Getting the message is
> no problem,
> > but getting it into the JavaScript is.
> >
> > <script language="JavaScript" type="text/JavaScript">
> > function displayMessage()
> > {
> >   var message = "MESSAGE HERE";
> >   if(message == "")
> >   {
> >   }
> >   else
> >   {
> >     alert(message);
> >   }
> > }
> > </script>
> >
> > <body onload="displayMessage()">
> >
> >
> > I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
> > #{mainMenu.alertMessage} would be an empty string or have a
> message. I am
> > getting var message = "#{mainMenu.alertMessage}" instead of the value of
> > #{mainMenu.alertMessage}.
> >
> > Is there a way using MyFaces to achieve this?
>
> I think in the next release of JSP this will be possible (EL expressions
> evaluated in the body of the JSP page). However that's no consolation
> for you now :-) [and I might be wrong anyhow...]
>
> Right now, the only place an EL expression #{..} is evaluated is in the
> attribute of a JSF tag, so your approach above won't work.
>
> You might be able to do something like this:
>    <t:div style="display:none" id="mymessage" forceid="true">
>      <h:outputText value="#{...}"/>
>    </t:div>
> to force some text to be output in a named div, but not visible to the
> user. Then in your javascript you could do:
>    var message = document.getElementById("mymessage").innerHtml();
>    alert(message);
>
> The alternative would be to write a component that can emit arbitrary
> text as a script variable, eg
>    <t:var name="message" value="#{...}"/>
> which generates something like:
>    <script>var message="..."</script>
>
> There's the t:stylesheet tag which is sort of in this spirit.
>
> Note that writing a custom JSP component is sadly much more complicated
> than it should be :-(
>
> Regards,
>
> Simon
>
> __________ NOD32 1.1282 (20051109) Information __________
>
> This message was checked by NOD32 antivirus system.
> http://www.eset.com
>
>


Re: JavaScript alert() window

Posted by Simon Kitching <sk...@obsidium.com>.
Warren Bell wrote:
> I want to create a JavaScript alert window with a message from a resource
> bundle when I have a message to display. Getting the message is no problem,
> but getting it into the JavaScript is.
> 
> <script language="JavaScript" type="text/JavaScript">
> function displayMessage()
> {
>   var message = "MESSAGE HERE";
>   if(message == "")
>   {
>   }
>   else
>   {
>     alert(message);
>   }
> }
> </script>
> 
> <body onload="displayMessage()">
> 
> 
> I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
> #{mainMenu.alertMessage} would be an empty string or have a message. I am
> getting var message = "#{mainMenu.alertMessage}" instead of the value of
> #{mainMenu.alertMessage}.
> 
> Is there a way using MyFaces to achieve this?

I think in the next release of JSP this will be possible (EL expressions 
evaluated in the body of the JSP page). However that's no consolation 
for you now :-) [and I might be wrong anyhow...]

Right now, the only place an EL expression #{..} is evaluated is in the 
attribute of a JSF tag, so your approach above won't work.

You might be able to do something like this:
   <t:div style="display:none" id="mymessage" forceid="true">
     <h:outputText value="#{...}"/>
   </t:div>
to force some text to be output in a named div, but not visible to the 
user. Then in your javascript you could do:
   var message = document.getElementById("mymessage").innerHtml();
   alert(message);

The alternative would be to write a component that can emit arbitrary 
text as a script variable, eg
   <t:var name="message" value="#{...}"/>
which generates something like:
   <script>var message="..."</script>

There's the t:stylesheet tag which is sort of in this spirit.

Note that writing a custom JSP component is sadly much more complicated 
than it should be :-(

Regards,

Simon

RE: JavaScript alert() window

Posted by Yee CN <ye...@streamyx.com>.
The following should work:

<script language="JavaScript" type="text/JavaScript"> function
displayMessage(message) {
  if(message == "")
  {
  }
  else
  {
    alert(message);
  }
}
</script>

<body onload="displayMessage('#{mainMenu.alertMessage}')">

--------------------------
Or simply
<body onload="alert('#{mainMenu.alertMessage}')">


Cheers,
Yee

-----Original Message-----
From: Warren Bell [mailto:warren@clarksnutrition.com] 
Sent: Thursday, 10 November 2005 10:19 AM
To: users@myfaces.apache.org
Subject: JavaScript alert() window

I want to create a JavaScript alert window with a message from a resource
bundle when I have a message to display. Getting the message is no problem,
but getting it into the JavaScript is.

<script language="JavaScript" type="text/JavaScript">
function displayMessage()
{
  var message = "MESSAGE HERE";
  if(message == "")
  {
  }
  else
  {
    alert(message);
  }
}
</script>

<body onload="displayMessage()">


I want to replace "MESSAGE HERE" with #{mainMenu.alertMessage}.
#{mainMenu.alertMessage} would be an empty string or have a message. I am
getting var message = "#{mainMenu.alertMessage}" instead of the value of
#{mainMenu.alertMessage}.

Is there a way using MyFaces to achieve this?

Thanks,

Warren Bell