voyent
Javascript seems not to work  XML
Forum Index -> General Help
Author Message
hdbwcon

Joined: 14/May/2009 00:00:00
Messages: 93
Offline


Hello,

I have got here straightforeward JS-code in an XHTML-page that does not work.

Code:
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html>
 <head>
 <script type="text/javascript">
 /* <![CDATA[ */
  function test()
  {
   alert("Hello World!");
   document.foo.test.value="foo";
  }
  /* ]]> */
 </script>
 </head>
 <body>
 <f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:ice="http://www.icesoft.com/icefaces/component" xmlns:ui="http://java.sun.com/jsf/facelets">
 	<ice:portlet>
 		<ice:form name="foo">
 			<ui:define  name="utf8">
 		   		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 			</ui:define>
 			<ice:outputStyle href="/xmlhttp/css/xp/xp-portlet.css" />
 			<ice:panelGrid columns="3">
 					<ice:inputText style="color: green;font-weight:bold;" value="Testfield"/>
 					<ice:inputText name="test" id="test"/>
 					<ice:commandButton type="submit" value="test-JavaScript" onclick="test()"/>
 			</ice:panelGrid>
 		</ice:form>
 	</ice:portlet>
 </f:view>
 </body>
 </html>
 
 

I have a commandbutton and wants that the function test() should be called when I click on it. But it seems not to work, nothing happens.
I tried it with test(); too but with no result.

I use ICEFaces 1.8.1 with the Liferay portal 5.2.2, but I am not sure if the problem is bounded to Liferay so I put my question to here.

Perhaps somebody knows what I do wrong.

Greetings


davidfg

Joined: 04/Feb/2009 00:00:00
Messages: 161
Offline


Use javascriptcontext
http://www.icefaces.org/docs/v1_6_0/javadocs/icefaces/api/com/icesoft/faces/context/effects/JavascriptContext.html
hdbwcon

Joined: 14/May/2009 00:00:00
Messages: 93
Offline


...my aim is not reached yet.
Thanks for the first step.
It is no problem to run with JavaScriptContext a JavaScript-code like alert.
But now I have a problem to assign a value to a field in the XHTML-page with JavaScript.I want to assign the value foo to the field password in the form reg wthe the user clicks on the commanbutton. I added to the form the id and name reg and to the outputText the id and name password but nothings happens when I cklick the button.

My XHTML-Page:

Code:
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html>
 <head>
 </head>
 <body>
 <f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:ice="http://www.icesoft.com/icefaces/component" xmlns:ui="http://java.sun.com/jsf/facelets">
 	<ice:portlet>
 		<ice:form id="reg" name="reg">
 			<ui:define  name="utf8">
 		   		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 			</ui:define>
 			<ice:outputStyle href="/xmlhttp/css/xp/xp-portlet.css" />
 			<ice:panelGrid columns="2">
 					<ice:outputText name="password" id="password" value="#{Test.password}"/>
 					<ice:commandButton actionListener="#{Test.test}" value="test" />
 			</ice:panelGrid>
 		</ice:form>
 	</ice:portlet>
 </f:view>
 </body>
 </html>
 


My Bean Test

Code:
 package test;
 
 import javax.faces.context.FacesContext;
 import javax.faces.event.ActionEvent;
 
 import com.icesoft.faces.context.effects.JavascriptContext;
 
 public class Test {
 
 public void test(ActionEvent e)
 {
 		
 //works perfectly
 JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(),"alert('Hallooooooo')");
 		
 //does not work
 JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "document.reg.password.value=foo");
 		
 	}
 
 }
 
 


Has somebody an idea why?

Greetings
venu_dvmr

Joined: 30/Sep/2008 00:00:00
Messages: 58
Offline


In JSF HMTL from elements ID's and Names are generated dynamically so you have to reference in JS using those names.
hdbwcon

Joined: 14/May/2009 00:00:00
Messages: 93
Offline


Thanks so the names are important for JS, I understand.

But

neither

Code:
 
 
  JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "document.reg.password.value=foo")
 


nor

Code:
 
 String call = "document.getElementById('reg:password').value='foo'";
 		JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(),call);
 


works for me.

Has somebody an idea why?
 
Forum Index -> General Help
Go to:   
Powered by JForum 2.1.7ice © JForum Team