Benas
Benas

Reputation: 2332

JSF 1.2 text to Javascript text. How to escape backslashes

I have this text in my JSF bean:

"\\name"

On JSF page it looks like this:

"\name"

Now, I want pass this string to the javascript function

 function showAlert(name) {
     alert(name);
 }

 <h:commandLink onclick="showAlert(#{myData.name})">
     <h:outputText value="#{myData.name}"/> 
 </h:commandLink>

However in firebug console I get error, which says that :

 SyntaxError: illegal character
 var a=function(){showAlert(\\name);};var b=function...

What is wrong with this code? One backslash escapes other backslash, however I still get this error.

How can I fix this?

Upvotes: 0

Views: 292

Answers (1)

Gimby
Gimby

Reputation: 5283

The problem is simply that the generated javascript is lacking quotes to make the value a javascript string. Ex after JSF/Java has finished constructing the view, the resulting javascript call that ends up in the page content processed by the browser needs to be this:

var a=function(){
  showAlert('\\name');
};

Upvotes: 1

Related Questions