• Post Reply Bookmark Topic Watch Topic
  • New Topic

Silly little Javascript problem but can't figure it out

 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I rarely use Javascript (hence what is almost bound to be a bit of a daft
question) and have been trying to get this simple function to work to highlight
rows on a dataTable in JSF:



This code is located in the page header. However for some strange reason I'm getting this
error in the Glassfish server log (I'm using JSF Mojarra 2.0.3):

Error Traced[line: 33] The content of elements must consist of well-formed character data or markup.
I also get this error when using Check XML in NetBeans 6.9.1.

- where line 33 is this line: "for(var i = 0; i < trs.length; i++) {". The reason seems to be that
it doesn't like the less than character. If I say "trs.length > i" it works fine! I'm starting to think I'm
going a bit crazy here, can anyone point out what it is that I'm doing wrong? I hope I've not gone
off the silly scale...

Thanks,
Brendan.

p.s. database highlighting code from BalusC http://balusc.blogspot.com/2006/06/using-datatables.html
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
410
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your JavaScript is fine. It looks like something is trying to parse the JavaScript as XML. I'm going to move this to our JSF forum in case someone knows if JSF is doing this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like it's tripping up some XML validator somewhere. Is this just an IDE warning, or does it actually cause the page not to load? Is the page being served as XML or HTML (and no, just making the page XHTML does not cause it to be served as XML).

And: new Function("this.bgColor='#ff0000'"); Seriously?

Code in strings is anathema. Prefer:
Also, bgColor is deprecated. Use CSS for styling.

[Edit: Jeanne pulled the trigger faster. ]
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the standard practice is to embed JavaScript in CDATA tags. If you're just getting a validation warning, that might clear it up.

 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok, thanks for the tips, I've now got this:



Thanks you've all been a big help. I don't feel quite so silly now, I mean you're not going
to accidentally stumble across a syntax like "//<![CDATA[". I've seen this around however
the significance, and importance, had passed me by. It's all working now, but what a crazy
side effect!

Thanks.
Brendan.

p.s. I'm gonna get a good JS book very soon.
 
Greg Charles
Sheriff
Posts: 3010
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad that helped. Yes, it's an artifact of mixing languages in a single page. JavaScript isn't valid XML, but if we stuff it CDATA tags, we tell the XML validator that it's just a big block of text and that it shouldn't worry about it.

Personally, I hate writing regular JavaScript, and I've had much better luck with JQuery. Bear might be able to recommend a good book on that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!