0

I am working on a web-app that is using JSF 2.1 and RichFaces 3.3

I have an external piece of Javascript that needs to run across all of the applications' pages, so I put it in the head of my template file, call it layout.xhtml,

This is used as the template by all of the different pages, so I think I've put it in the right place at least.
<ui:composition template="/templates/layout.xhtml">

The script itself is okay, tested it on JSFiddle and it runs as expected.

Its format is like so:

<script src="https://cdn.cookielaw.org/scripttemplates/otSDKStub.js" 
        data-document-language="true" 
        type="text/javascript" 
        charset="UTF-8" 
        data-domain-script="dummy-value-for-stack-overflow">
</script>

When testing within the applcation, I was able to get the script to run fine on a normal .html page, however when I change to using a .xhtml page I am getting an error in the browser's console:

Uncaught (in promise) TypeError: e.Groups.forEach is not a function
    at Ct.setPublicDomainData (otBannerSdk.js:7)
    at Ct.<anonymous> (otBannerSdk.js:7)
    at otBannerSdk.js:7
    at Object.next (otBannerSdk.js:7)
    at otBannerSdk.js:7
    at new Promise (<anonymous>)
    at c (otBannerSdk.js:7)
    at Ct.initializeBannerVariables (otBannerSdk.js:7)
    at otBannerSdk.js:7
    at otBannerSdk.js:7

I think this is due to the data attributes in the script tag, data-document-language and data-domain-script, as they aren't valid attributes in any of the namespaces I'm using and XHTML is strict about that sort of thing:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich"
    lang="en-US">

What I'm thinking is that XHTML doesn't like those data attributes, so the script is trying to run without them, and the console error is arising from a null value.

Is there any way around this? I know that JSF 2.2+ allow for some custom attribute options but I wasn't sure they applied to script tags specifically

It's my first time working on a JSF project so apologies if I've missed anything, thanks.

4
  • 1
    Please read stackoverflow.com/questions/2935759/… first... I even think it is a duplicate. If it was a server side namespace issue, the server would have complained, not an error in the client. I rather think it is a basic conflict between duplicate .js files being loaded. What is relevant is what the client-side html looks like, not what it server side is and what the client-side error is. Try removing the xmlht namespace like in the duplicate Commented May 13, 2020 at 18:54
  • You say duplicate .js files being loaded, however in the network tab of the browser, I don't see any duplicates, just a normal GET request with a Status 200 response Commented May 14, 2020 at 11:09
  • there must be multple get requests since you use richfaces which also uses java. But I mentioned more... Check differences, make a plain xhtml file without richfaces components. Stll errors? Make a minimal reproducible example then... And post the error in a searchengine and investigate... There are things about object vs array etc.. Commented May 14, 2020 at 13:17
  • java should have been javascript in my previous comment Commented May 14, 2020 at 16:11

1 Answer 1

1

As it turns out, a conflict of some sort occurred as RichFaces was injecting a number of scripts into the head portion of the html file, and seemed to cause some strange behaviour with the script I was trying to add.

A workaround was to make a small change to my web.xml, changing the RichFaces LoadScriptStrategy value from ALL to DEFAULT

    <context-param>
        <param-name>org.richfaces.LoadScriptStrategy</param-name>
        <param-value>DEFAULT</param-value>
    </context-param>

NONE also worked for me but is advised to be avoided as per their documentation: https://docs.jboss.org/richfaces/latest_3_3_X/en/devguide/html/ArchitectureOverview.html#ScriptsandStylesLoadStrategy (getting rid of all of these scripts will probably cause something else to break)

Credit to @Kukeltje for his comments trying to clarify my problem - without that I wouldn't even have considered that RichFaces was the culprit, I was convinced it was an issue with xhtml and tags (it wasn't)

Sign up to request clarification or add additional context in comments.

1 Comment

See why creating a minimal reproducible example is always relevant... Always... it helps a lot in finding root causes or at least narrowing things down tremendously... Always try to create one...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.