Forum Moderators: open

Message Too Old, No Replies

JavaScript Validation

What's wrong with this code?

         

pageoneresults

3:22 pm on Jul 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Errors
Line 141, character 106:
... /"+gDcsId+"/wtid.js"+"'></SCR"+"IPT>");
^Error: delimiter """ invalid: only s and tagc allowed here
Line 141, character 106:
... /"+gDcsId+"/wtid.js"+"'></SCR"+"IPT>");
^Error: end tag for element SCR which is not open; try removing the end tag or check for improper nesting of elements

I'm attempting to work with the new advanced JavaScript code for WebTrends On Demand. Why can't these people generate valid code? ;)

Fortunately for me, I don't have to deal with much JavaScript. But, when I do, there are common validation errors that I can normally correct myself, usually escaped ampersand issues.

I have no clue what may be happening above and below. I'm going to guess that I need to escape something in the string somewhere. Any help would be appreciated.

<script type="text/javascript"><!--
var gImages=new Array;
var gIndex=0;
var DCS=new Object();
var WT=new Object();
var DCSext=new Object();

var gDomain="statse.webtrendslive.com";
var gDcsId="removed";

if ((typeof(gConvert)!="undefined")&&gConvert&&(document.cookie.indexOf(gFpc+"=")==-1)){
document.write("[b]<SCR"+"IPT[/b] Language='JavaScript' SRC='"+"http://"+gDomain+"/"+gDcsId+"/wtid.js"+"'>[b]</SCR"+"IPT>[/b]");
}
//-->
</script>

Why would they break the word SCRIPT like that?

rocknbil

6:11 pm on Jul 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because for whatever reason you can't document.write('<script>') tags. They just won't execute. Breaking it up fakes it and it works.

I don't know if this helps - but validators will fail on Javascript writes if you don't escape slashes, so everywhere you see a / change it to \/, see if that works.

kaled

7:05 pm on Jul 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a general rule, when creating complex strings, create them, then display them with alert() then use them. When the code is working correctly, remove the calls to alert().

I always place <!-- on a new line for javascript.

You should use type="text/javascript" rather than language="javascript"

In this case, escaping / chars is complex. Because the strings are going to be written to the page, each / char probably needs to be escaped with three \ chars.

Thus I think "http://" must be written as "http:\\\/\\\/".

Perhaps only double-escaping is required - I was bug-hunting till 4am last night and my brain's only firing on two cylinders.

Kaled.

pageoneresults

3:51 pm on Jul 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks rocknbil and kaled. All I had to do was add one \ to escape the / and it validated fine. Now, whether or not the script still works is another question. I would assume all is okay and I'll keep an eye on it over the next week or so.

if ((typeof(gConvert)!="undefined")&&gConvert&&(document.cookie.indexOf(gFpc+"=")==-1)){
document.write("<SCR"+"IPT Language='JavaScript' SRC='"+"http://"+gDomain+"/"+gDcsId+"/wtid.js"+"'><[b]\/SCR"+"IPT>[/b]");