Asynchronous java script loading is showing error

3.3k views Asked by At

Asynchronous java script loading is showing the following error:

Attribute name "async" associated with an element type "script" must be followed by the ' = ' character. + jsf

I found this in my JSF2.4 application which uses template file template.xhtml. The DOCTYPE is

<!DOCTYPE html  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:p="http://primefaces.org/ui">

This error goes when I use HTML 5 DOCTYPE but it causes some other errors since XHTML DTD is not found.

So I tried attribute async="async" instead of just using async. Then all the errors vanished.

Is this the right method to load java script asyncronously in the context like this? Please let me know if you have better methods to fix this.

1

There are 1 answers

3
dlopez On BEST ANSWER

As you can read here https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script, "async" is an HTML5 attribute, so you must use another way to load your javascript asynchronously.

One trick can be to move your script just before the closing tag.

You can also use use "require.js", as this answer suggest: Load async resource with requirejs timeout