javascript in jsf xhtml template - javascript

Trying to put the following in my JSF xhtml template
<script type="text/javascript">
function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
</script>
I substituted the && with &amp&amp and &ux0026&ux0026 but they don't seem to work
The error I get is: Error Parsing /templates/template.xhtml: Error Traced[line: 8] Element type "a.length" must be followed by either attribute specifications, ">" or "/>".
Does anyone if any suggestions/ideas how to get around this?

The problem is with < in i<a.length, you should escape it too (<), or use CDATA as you said.
Also see this related question: javascript in jsf/icefaces

Related

Print managed bean property as JavaScript variable

I'm trying to print java string return values into script java .
this is my method in java class
public static String getchamps (){ //Bdconnection class
String str;
{
str = "date" ;
}
return str ;
}
the question is how can i have this code
<script type="text/javascript" >
"date"
</script>
You should in this context look at JSF as a HTML code generator. You can use JSF tags/components and EL expressions to let it produce the desired HTML output. As JavaScript is as being a client side language technically part of HTML (not of JSF!), you can just do the same for JavaScript.
Once you fix that bad static method to really comply the Javabeans specification as below:
public String getChamps() {
return "date";
}
Then you can just let JSF print it as if it's a JS variable as below:
<script>
var champs = "#{bean.champs}";
</script>
To verify the result, just open the JSF page in webbrowser and do rightclick, View Source. You should see something similar to:
<script>
var champs = "date";
</script>
Beware of JS-special characters in the string though! This would fail if the string contains e.g. a doublequote or even a newline. If that's the case, then you'd better escape it using an EL function as shown in this answer: Escape JavaScript in Expression Language.
Do this:
<h:inputText id="propertyId" value="#{Bdconnection.str}" />
and access in script
document.getElementById('propertyId').val();

How can I get the body contents out of a variable containing HTML?

I have a variable htmlSource containing HTML code like this:
<!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">
<head>
<title>IIS 8.0 Detailed Error - 404.0 - Not Found</title>
</head>
<body>xxx some code here yy</body>
</html>
How can I create a new variable htmlBodyOnly that contains only "xxx some code here yy". If possible I would like to do this with a regular expression. I am just not sure how to exclude the start and end using a regex or something similar.
Sorry but I don't have jQuery to use to help. I am working just on a javascript variable. Not working on the DOM.
This is ugly, but you can keep it as a string with this method:
htmlsource.substring(htmlsource.indexOf("<body>")+6, htmlsource.indexOf("</body>"))
The +6 is because the string "<body>" has 6 characters and the indexOf method returns the index of the first character in the string to search for.
Here's proof that it works given your example: http://jsfiddle.net/9wBkf/
This assumes that the body tag will have no attributes i.e. <body class="myClass>
You can use a DOMParser to parse the html and extract the content of the body. See this SO question: Converting HTML string into DOM elements?
var parser = new DOMParser()
var doc = parser.parseFromString(stringToParse, "text/html")
console.log(doc.body.innerHTML)
Here is a Fiddle!
I do not know which regular expression you can use for that, but I think I know an alternative solution. You can also 'convert' your var to a DOM-object and then read the body-child.
Converting HTML string into DOM elements?

Markup Validation error in Javascript

I am getting this error in my markup validation:
Warning Line 23, Column 23: character "<" is the first character of a delimiter but occurred as data
for (var i = 0; i < array.length; i++) {
Is there something wrong with the "<" character in the following js?
function preloadImages(array) {
if (!preloadImages.list) {
preloadImages.list = [];
}
for (var i = 0; i < array.length; i++) {
var img = new Image();
img.src = array[i];
preloadImages.list.push(img);
}
}
It looks like your document is XHTML and your Javascript is inlined. At least you seem to be validating against XHTML. Since XHTML is rather strict and closer to XML, < is interpreted as the start of a new tag and confused the validator.
There is a number of ways to fix this:
Use HTML5 instead of XHTML; really, this usually is the best choice unless you have a good reason to stick to XHTML. It is understood by all major browsers and is the latest and most modern doctype that allows all the cool new features of the Web 2.0. The doctype for HTML5 looks like this:
<!DOCTYPE html>
<html>
....
Don't inline your Javascript, but add it as an external resource. You should be doing this all the time anyway, no matter what doctype you are using:
<script type="text/javascript" src="yourScript.js"></script>
Enclose the script in a CDATA block:
<script type="text/javascript">
<![CDATA[
// your code
]]>
</script>
You can read up on doctypes and XHTML for further details.

Javascript not-well formed error in for loop

I copied this code from an example. I've read it 100 times.
Array.prototype.map = function(fn) {
var r = [];
var l = this.length;
for(var i = 0; i < l; i++) {
r.push(fn(this[i]));
}
return r;
};
Why does Firefox say:
not well-formed
file:///some/path.html Line: 5
for(var i = 0; i < l; i++) {
-------------------^
UPDATE
The error is only shown when Firebug is turned on for the page.
You are using Javascript code in an HTML page claiming to be fully XHTML-compliant. Therefore, the < character cannot appear in the Javascript, as it would be interpreted as the beginning of an XHTML tag.
There are a number of ways to fix this.
You could change the DOCTYPE and make it not XHTML.
You could enclose the Javascript in a <![CDATA[ section.
You could move the Javascript into a separate .js file.
You could escape every occurrence of < with &lt and every & with &. I do not recommend this option; it'll make your code unreadable and will almost definitely not work in IE.
Likely your error isn't in this code, but something above it trickling errors down. So, instead of finding an error in this code, look above for malformed HTML or javascript that could be causing this error instead.
If I use the following HTML and your text as "test.js", I too get no errors in Firebug.
<html>
<head>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
test
</body>
</html>
Works for me. Please post the full file, and make sure you are using script tags.
I posted a validating pastebin file (Chetan's was technically not valid), and it works fine with Firebug. So I suggest you come back with a full page of validating code that doesn't work.

javascript validation error

here's my code, which says error when i run it through w3.validator.org.
for (var i = 0; i < CheckBox.length; i++)
error message -
character "<" is the first character of a delimiter but occurred as data
how do i fix this?
If you are "using" XHTML then you need to wrap the code in CDATA:
<script type="text/javascript">
//<![CDATA[
// rest of your javascript goes here
//]]>
</script>
Is your input XHTML? If so, to validate it as XML you would need a CDATA block to contain your javascript code.

Categories