JSF CommandLink malfunctions on Firefox when reRendering an entire form

I am currently working on a JSF 1.2 application (Sun RI, Facelets, Richfaces) that was previously designed only for IE6 browsers. However, we now need to extend our support to include Firefox as well.

On one of the pages, there is a form with a button that triggers a re-render of the entire form. Subsequently, after this re-rendering process, some links (<h:commandLink/>) are dynamically added to the form.

The JSF code snippet in question is:

<h:form id="foobar">
    ...
    <a4j:commandButton ... reRender="foobar"/>
    ...
    <h:commandLink .../>
</h:form>

One issue I encountered relates to the HTML code generated by the command link component, specifically:

<a href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['foobar'],'...','');}return false">bla bla</a>

(just to clarify, jsfcljs is a Javascript function created by the <h:commandLink/> component)

The problem arises because document.forms["foobar"] functions correctly when the page is initially loaded, but becomes dysfunctional after the form is re-rendered following an Ajax call, particularly on Firefox (while it still works on IE6).

This results in a JavaScript error when attempting to click on any link within the form post-Ajax call.

It's worth noting that if I use

document.getElementById("foobar");
instead, Firefox successfully locates my form even after the Ajax call.

To provide more context, let's consider the following custom Javascript function:

function test() {
    var e = document.forms;
    var tmp = "";
    for (i = 0; i < e.length; i++) {
        tmp = tmp + e[i].id + " ; ";
    }
    alert(tmp);
}

When running this function before and after the Ajax call, the following outcomes are observed:

someForm ; anotherForm ; foobar ; // Before Ajax call on FF 
someForm ; anotherForm ; // After Ajax call on FF. ISSUE HERE!

someForm ; anotherForm ; foobar ; // Before Ajax call on IE6 
someForm ; anotherForm ; foobar ; // After Ajax call on IE6

My analysis of the problem suggests that upon receipt of the Ajax response, a4j removes the reRender-ed elements (including my foobar form) from the DOM tree objects. Consequently, this removal from the document.forms array update is not propagated in Firefox as opposed to IE6 which does update the array. As a result, document.forms["foobar"] returns undefined post-Ajax call on Firefox.

As a workaround solution, I recommend adjusting the reRender attribute to target specific subsections of the form rather than the entire form itself. This adjustment ensures the proper functionality of the links.

However, I'm curious to explore other potential resolutions for this issue without requiring modifications to the reRender attribute. Any suggestions?


Edit

The Javascript snippet triggered when a command link is clicked is as follows:

function dpf(f) {
    var adp = f.adp;
    if (adp != null) {
        for (var i = 0; i < adp.length; i++) {
            f.removeChild(adp[i]);
        }
    }
};

function apf(f, pvp) {
    var adp = new Array();
    f.adp = adp;
    var ps = pvp.split(',');
    for (var i = 0, ii = 0; i < ps.length; i++, ii++) {
        var p = document.createElement("input");
        p.type = "hidden";
        p.name = ps[i];
        p.value = ps[i + 1];
        f.appendChild(p);
        adp[ii] = p;
        i += 1;
    }
};

function jsfcljs(f, pvp, t) {
    apf(f, pvp);
    var ft = f.target;
    if (t) {
        f.target = t;
    }
    f.submit();
    f.target = ft;
    dpf(f);
};

Within the <h:commandLink> onclick handler, the invocation is made to

jsfcljs(document.forms['foobar'], 'someId', '')
causing evaluation to jsfcljs(undefined, 'someId', ''). This sequence leads to a JavaScript error stating that f is undefined upon execution of f.

Answer №1

It appears that the issue stems from HTML.

Make sure to define your form using a name attribute, not just an id:

Keep in mind that different browsers may interpret name and id attributes differently. If changing this doesn't resolve your problem, feel free to let me know and I can create a test scenario without needing server-side code to demonstrate the issue you're facing.

An alternative approach to resolving this issue is to update inline onclick event calls on the client-side to reference the form by id using document.getElementById("foobar") instead of document.forms["foobar"]

Answer №2

I remember encountering this issue with Sun Mojarra 1.2, which has been around for about 4 years now. Updating the libraries should help resolve this issue. The most recent version of Sun Mojarra 1.2, 1.2_14, was released just three weeks ago. Simply swap out the two JSF libraries in your /WEB-INF/lib directory with these updated versions.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn the technique in Vue to separate a string using commas and store the values in an array

As a Ruby enthusiast, I am delving into the world of Vue. In my project, users can input multiple styleCodes separated by commas. I aim to store these styleCodes in an array for flexible length calculations. See my code snippet below: <template> &l ...

Exploring the process of traversing a function for each ng-repeat element

Hey everyone, I'm currently facing an issue where I need to pass each date through a function in order to get the desired result. Let's take a closer look at the code snippet: <md-list> <md-divider ></md-divider> ...

Error: Visual Studio Code is unable to locate the module 'typegram/callback'

Currently, I am developing a telegram bot utilizing the telegraf package (version 4.1.1). Everything was functioning perfectly until I incorporated additional modules from the telegraf package such as Extra and mark-up. Unfortunately, I encountered the f ...

Guide on inputting information into a dual-column table where one column is linked to the other

After successfully inserting data with hardcoded values to verify the relation between the 2 columns, I am now wondering if there is a way to reference the value of id in reply_id. This is how I manually inserted data: const { data, error } = await su ...

converting a CSV string into a JSON array using AngularJS

My data is in a comma-separated format: "HotelName","Remained","Date" "Maxx","4","Jun 26 2016" "Voyage","3","Jun 24 2016" I am looking to convert this into a JSON array as shown below. How can I achieve this using my JavaScript code? [ { HotelName ...

What is the best way to transform a collection of items into FormData?

In my current project, I have a JavaScript array structured as follows: var items = [{ ID: "1" count:'1', File: (binary file) }, { ID: "2" count:'2', File: (binary file) } ] My goal is to ...

What is the process of calculating the average of JSON data and looping through it multiple times using Javascript?

I've encountered a JSON data set that has the following structure: { name: "Evelyn", assignment: "SCRUM", difficulty: 3, fun: 4 } And so on. My goal is to calculate the average value of both difficulty and fun for e ...

Locate a specific data point within an array of JSON objects

After receiving an array of JSON objects from JSP, I now have a set of data that contains book titles. "Titles":[ { "Book3" : "BULLETIN 3" } , { "Book1" : "BULLETIN 1" } , { "Book2" : "B ...

Preserving the information from a webpage by utilizing casperjs for scraping and saving table data

What is the most efficient way to preserve table data collected during a web scraping process with casperjs? Saving it as a file after serializing into a json object. Sending an ajax request to php and then storing it in a mysql database. ...

Ways to incorporate a dynamic HTML form that allows for input elements to be added both horizontally and vertically while maintaining connected lines

Looking to design a form that showcases HTML elements in both vertical and horizontal positions, with lines connecting them as seen in this example: https://i.sstatic.net/jB12f.png. Can anyone offer guidance on how to achieve this? Thanks! ...

Obtain the current date using Moment JS in JavaScript

Here is a scenario with code : let currentTime = moment(); console.log(currentTime.format()); // 2019-11-25T20:23:50+02:00 console.log(currentTime.toDate()); // 2019-11-25T18:23:50.916Z After applying the timezone change on Heroku using the command ...

Error message: "Issue with Jointjs library on Node.js server - Uncaught ReferenceError: Joint is not recognized"

I am attempting to create a sample diagram using the code below on a file hosted on a Node server: <!DOCTYPE html> <html> <head> <title>newpageshere</title> <link rel="stylesheet" href="joint.css" /> <script src="j ...

Move to the following <article>

I am currently working on developing a JavaScript function that will automatically scroll to the next article whenever the down arrow key is pressed. The challenge I am facing is that my HTML elements are all dynamic and are simply article tags without any ...

Stop GIFs from playing automatically

Looking for a solution to disable autoplay for animated gifs on my chat-site (php-based). Tried script below but out of ideas: <script> myVid=document.getElementsByTagName('img'); function disableAutoplay() { myVid.autoplay=false; m ...

CodeMirror version 5.62.3 is experiencing some challenges with the scrollbar functionality, editor size, and line wrapping

During my HTML/CSS/JS coding session, I encountered a challenge with CodeMirror version 5.62.3. Specifically, I was striving to make the scrollbar visible in the code editor, using C# as the language mode. However, despite setting the editor's height ...

Can you please provide the appropriate PropTypes for a dictionary in a ReactJS project?

Looking for something along the lines of y = {"key0": [value0, value1], "key1":[value2]} What is the proper way to define the proptypes for y? ...

Attempting to call setState (or forceUpdate) on a component that has been unmounted is not permissible in React

Hello everyone! I am facing an error message in my application after unmounting the component: Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, canc ...

Execute a JavaScript function to submit a form using MVC and jQuery, all without the need for a page

As someone who is fairly new to this, I am currently using ASP.NET MVC C# LINQ to SQL. I have an edit page where Authors and all their Books are loaded. The Books are fetched through an Ajax call. <script type="text/javascript"> $(docum ...

Transferring data from a text area to a table with AngularJS

Creating a C# MVC App with AngularJS My goal is to populate a table with values entered in a text area using Angular. Here is the process: Users input values into a text area like this: A B C When a user clicks a button, a modal window should open and ...

Getting row data in datatables after a button click: A step-by-step guide

Could somebody provide assistance with retrieving a single row of data on a click event? This table is dynamically populated after the AJAX call's Success function is executed <div class="table-responsive table-wrap tableFixHead container-flu ...