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

Steps to include a delete option on individual rows in jQuery datatables

Currently, I am working on implementing a jQuery DataTable with AJAX sourced data for my project. The HTML structure of my table is as follows: <table id="dynaFormVersionTable" class="table table-striped table-hover dt-responsive" cellspacing="0" widt ...

Vue.js - Resetting child components upon array re-indexing

I am working with an array of objects const array = [ { id: uniqueId, childs: [ { id: uniqueId } ] }, { id: uniqueId, childs: [ { id: uniqueId } ] }, ] and I have a looping structure ...

Fetching the second item within an object using JavaScript

I am trying to retrieve the data from the last month of an API, but I want to avoid hard-coding the date like this: const data = [data.data['Monthly Time Series']['2021-11-30']]. I need a way to dynamically access the 2nd object without ...

What is the correct way to incorporate scrollIntoView() using jQuery?

I'm trying to implement a jQuery function that will enable the last reply to scroll into view. This is my current code: function displayLastReply(replies){ replies.each(function() { if($(this).index() < nIniRep || afterReply){ $( ...

What is the best way to retrieve data from the next page in a Protractor test?

I am currently in the process of automating the test for booking a flight. When I enter the credentials on the homepage, click the Submit button, and the browser navigates to the search results page. const EC = protractor.ExpectedConditions; describe( ...

What impact does setting a variable equal to itself within a Dom Object have?

Within my code example, I encountered an issue with image sources and hrefs in a HTML String named tinymceToHTML. When downloading this html String, the paths were set incorrectly. The original image sources appeared as "/file/:id" in the String. However, ...

Why am I unable to access the array once the loop has finished?

While utilizing the Google Maps API and AngularJS (1.5.8), I encountered an issue where I couldn't access markers that were created in a loop. The code snippet below is located inside the initMap function: var markers = []; for(var i=0; i<10; i++ ...

How to access iFrame in ReactJS using document.getElementById

I am facing a challenge on my website where I need to transfer data (using postMessage) to an iframe. Typically in plain JavaScript, I would use techniques like document.getElementById or $("#iframe") in jQuery to target the iframe. However, I am unsure ...

Unresolved Issue: Jquery Modal Fails to Activate on Subsequent Click for Ajax-

When I make an Ajax call, I load HTML into a div. This HTML content contains a jQuery modal that opens when clicked. However, on the first click, the modal opens correctly. On subsequent clicks, I receive the following error in the console: Uncaught Type ...

Breaking down objects and setting default values

In need of assistance with resolving an issue related to default parameters and object destructuring. The 'product' object that I am working with has the following structure: { name: "Slip Dress", priceInCents: 8800, availableSizes ...

Exploring SVG Morphing Reversal Techniques in Anime.js

I have been trying to implement direction: 'reverse' and timeline.reverse(), but so far it hasn't been successful. Interestingly, when I set loop: true, the reverse animation can be seen within the loop. However, my goal is to trigger this a ...

Discover the process for finding a Youtube Channel Name with querySelectorAll

OUTPUT : Console URL : https://www.youtube.com/feed/trending?gl=IN document.querySelectorAll('a[class="yt-simple-endpoint style-scope yt-formatted-string"]')[0].innerText; document.querySelectorAll('a[class="yt-simple-endpoi ...

Expo background fetch initialized but not activated

During the development of my React Native app, I encountered the need to perform periodic background fetches from another server. To achieve this, I utilized two classes from Expo: import * as BackgroundFetch from 'expo-background-fetch'; import ...

Best Practices for Implementing Redux Prop Types in Typescript React Components to Eliminate TypeScript Warnings

Suppose you have a React component: interface Chat { someId: string; } export const Chat = (props: Chat) => {} and someId is defined in your mapStateToProps: function mapStateToProps(state: State) { return { someId: state.someId || '' ...

What are effective solutions to reduce the increasing Next.js bundle size caused by dynamic component lookup?

tldr: For more information, please visit the repository. The common.js file includes all dependencies, even though only one is used on the current page. http://localhost:3000/components/ComponentOne http://localhost:3000/components/ComponentTwo Live dem ...

The number of arguments provided is incorrect: 3 arguments were given, but only 0 or 1 are

Here is the model structure I am working with: class User < ActiveRecord::Base has_many :appointments has_many :doctors, :through => :appointments end class Doctor < ActiveRecord::Base has_many :appointments has_many :users, :through => :appo ...

Exploring the nesting of client components in Next.jsIf you are

Exploring the realm of NextJS and React, I find myself delving into the realm of client components. One such client component I'm working with is called Form.jsx. It looks something like this: export default function FormHome() { ... a plethora of ...

If a DOM element contains any text node, completely remove the element

Currently, I am using WordPress and my theme automatically generates a menu where all the items are marked with "-". It can be quite annoying. I have tried to fix it by replacing all the option values instead of just removing the "-", but I couldn't g ...

The data for my registration is not getting stored in the Firebase database as expected

I'm currently working on a registration app and it's my first time using firebase. My email address is successfully stored in the authentication section of firebase. However, when I try to connect my app to the database to store additional infor ...

Customize the active index in Primefaces tabview when switching tabs

I'm currently working on a tab view with two tabs. Whenever I switch from tab 1 to tab 2, I have some code that runs validation and updates certain values. Depending on the outcome of this validation, I want to either stay on tab 1 or move to tab 2 w ...