build javascript on the fly using java.

Hi i have an urgent requirement to be able to generate some javascript on ther fly and then output it to a broswer. This is the situation:

I've got a JSP that has a custom tag that will call an AppyOnChange method within a java class. This method was written by someone else previously and i have to build on it. Currently the method generates the javascript and outputs it to a broswer for an onchange event for when an onchange event occurs on a drop down list. what i need to do is update this so it can generate Javascript that will check the event that has caused the the method to be called. for example in the broswer when the onchange event is fired a javascript function will be called, this javascript function is one that we are generating in the java code. the function should determine what action (by action i mean mouseclick, if the enter key was pressed etc.) caused the funtction to be called and then perform the required action. Here is the requirement in point form on entrance to the java script function:

1. check the event that caused the javascript to be called;

2. if it wasa mouse click then send the requuest to the server;

3. else if it was a keypress and it was either an up or down arrow then just simply navigate through the drop down list, if the keypress was the nter button then send the request to the server.

This is the code that we haven in place that works for just simply that works for a mouse click and sends the request to the server. It doesn't work to handle keypresses or check if it wasa  keypress or mouse click:

protected static void applyOnChange(UIField field, UIInput inputComp) {
        StringBuffer buf = new StringBuffer();
        Object onchange = field.getAttributes().get("onchange");
        if (onchange != null) {
            buf.append(onchange);
        }
        String onchangeAction = field.getOnchangeAction();
        if (onchangeAction != null) {
            FacesContext context = FacesContext.getCurrentInstance();
            MethodBinding methodBinding = context.getApplication().createMethodBinding(onchangeAction, null);
            UIAction action = new UIAction();
            action.setId(UIField.ON_CHANGE_ACTION_ID);
            action.setAction(new TaskMethodBinding(methodBinding));
            if (field.isOnchangeActionImmediate()) {
                action.setImmediate(true);
            }
            field.getChildren().add(action);
           
            String formId = ComponentUtils.findAncestorForm(field).getClientId(context);
            String actionId = action.getClientId(context);
            String script = "executeAction('" + formId + "', '" + actionId + "')";
            if (buf.length() > 0) {
                buf.append("; ");
            }
            buf.append(script);
        }
        inputComp.getAttributes().put("onchange", buf.toString());
    }

I know this decription of the problem maybe long winded and maybe slightly confused but i have tried to include as much information as possible. Help on this ASAP would be greatly appreciated.
scurtis_1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bloodredsunCommented:
Are you using JSF or just vanilla JSP?
bloodredsunCommented:
Sorry, I've just looked closer at your code and can see that it is java server faces so that was a dumb question. I'll have a look and get back to you.
scurtis_1Author Commented:
yeah thats right JSF - thanks a lot!!
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

scurtis_1Author Commented:
righto, i have a bit of a go and this is what i have come up with. You may ask why i haven't just tested it but the project doesn't  compile at the moment (not my end i may add :-):

protected static void applyOnChange(UIField field, UIInput inputComp) {
        StringBuffer buf = new StringBuffer();
        String jsFunctionBody = null;
        Object onchange = field.getAttributes().get("onchange");
        if (onchange != null) {
            buf.append(onchange);
        }
       
        String onchangeAction = field.getOnchangeAction();
        if (onchangeAction != null) {
            FacesContext context = FacesContext.getCurrentInstance();
           
            MethodBinding methodBinding = context.getApplication().createMethodBinding(onchangeAction, null);
            UIAction action = new UIAction();
            action.setId(UIField.ON_CHANGE_ACTION_ID);
            action.setAction(new TaskMethodBinding(methodBinding));
            if (field.isOnchangeActionImmediate()) {
                action.setImmediate(true);
            }
            field.getChildren().add(action);
           
           
            String formId = ComponentUtils.findAncestorForm(field).getClientId(context);
            String actionId = action.getClientId(context);
           
            //generate the Javascript that will carry out the appropriate action according
            //to the window event that trigged this method to be called.
            jsFunctionBody = "function doOnchange(){ " +
                      "if (window.event.keycode){" +
                              //check to see if it was an enter key being pressed that caused
                              //the event to fire.
                              " if(window.event.keycode == 13){" +
                                    "executeAction('" + formId + "', '" + actionId + "')" +
                              " }" +
                        //check to see if it was a mouse click that caused the event to fire.
                        "else if (window.event.click){" +
                               "executeAction('" + formId + "', '" + actionId + "')" +
                         " }";
                                          
                  //script = "executeAction('" + formId + "', '" + actionId + "')";
            /*if (buf.length() > 0) {
                buf.append("; ");
            }
            buf.append(script);*/
        }
        inputComp.getAttributes().put("onchange", jsFunctionBody.toString());
    }
}

dioes htis look like the sorta thing? Or is it anything that you could use to build on?

Cheers!!!
bloodredsunCommented:
that looks great but I think you are missing a final "}" to close the function
GhostModCommented:
PAQed with points refunded (500)

GhostMod
Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.