/    Sign up×
Community /Pin to ProfileBookmark

Xhtml valid javascript code

Hi, I have this problem I need to use this javascript code to store statistic information about my web site:
<script type=”text/javascript” language=”JavaScript”>
// Define the location of count.asp
// Using a path, you may use this code in any subfolder
var file=’stats/count.asp’;

var d=new Date();
var s=d.getSeconds();
var m=d.getMinutes();
var x=s*m;
var h=new String;
var c=new String;
var r=new String;
var f=new String;
var u=new String;
var fs=new String;
var b=new String;
f=” + escape(document.referrer);
if (navigator.appName==’Netscape’){b=’NS’;}
if (navigator.appName==’Microsoft Internet Explorer’){b=’MSIE’;}
if (navigator.appVersion.indexOf(‘MSIE 3′)>0) {b=’MSIE’;}
u=” + escape(document.URL); w=screen.width; h=screen.height;
v=navigator.appName;
fs = window.screen.fontSmoothingEnabled;
if (v!= ‘Netscape’) {c=screen.colorDepth;}
else {c=screen.pixelDepth;}
j=navigator.javaEnabled();
info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b=’ + b + ‘&x=’ + x;
document.write(‘<img src=”‘ + file + ‘?’+info+ ‘” width=0 height=0 border=0 />’);
</script>

When I try to validate Xhtml 1.0 Transitional the code, the validator send this error:

Line 189, column 18: cannot generate system identifier for general entity “h”

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

An entity reference was found in the document, but there is no reference by that name defined. Often this is caused by misspelling the reference name, unencoded ampersands, or by leaving off the trailing semicolon (?. The most common cause of this error is unencoded ampersands in URLs as described by the WDG in “Ampersands in URLs”.

Entity references start with an ampersand (&) and end with a semicolon (?. If you want to use a literal ampersand in your document you must encode it as “&amp;” (even inside URLs!). Be careful to end entity references with a semicolon or your entity reference may get interpreted in connection with the following text. Also keep in mind that named entity references are case-sensitive; &Aelig; and &aelig; are different characters.

Note that in most documents, errors related to entity references will trigger up to 5 separate messages from the Validator. Usually these will all disappear when the original problem is fixed.

&#9993;

Line 189, column 18: general entity “h” not defined and no default entity

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

This is usually a cascading error caused by a an undefined entity reference or use of an unencoded ampersand (&) in an URL or body text. See the previous message for further details.

&#9993;

Line 189, column 19: reference not terminated by REFC delimiter

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

If you meant to include an entity that starts with “&”, then you should terminate it with “;”. Another reason for this error message is that you inadvertently created an entity by failing to escape an “&” character just before this text.

&#9993;

Line 189, column 19: reference to entity “h” for which no system identifier could be generated

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

This is usually a cascading error caused by a an undefined entity reference or use of an unencoded ampersand (&) in an URL or body text. See the previous message for further details.

&#9993;

Line 189, column 17: entity was defined here

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

Line 189, column 30: cannot generate system identifier for general entity “c”

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

&#9993;

Line 189, column 30: general entity “c” not defined and no default entity

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

&#9993;

Line 189, column 31: reference not terminated by REFC delimiter

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

&#9993;

Line 189, column 31: reference to entity “c” for which no system identifier could be generated

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

&#9993;

Line 189, column 29: entity was defined here

info=’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b

Line 189, column 42: cannot generate system identifier for general entity “r”

…’w=’ + w + ‘&h=’ + h + ‘&c=’ + c + ‘&r=’ + f + ‘&u=’+ u + ‘&fs=’ + fs + ‘&b=’

How can I write variables correct with valid Xhtml? Some ideas Cetalfio

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayMay 03.2005 — How about:
<i>
</i>var amp=String.fromCharCode(38);
....
info='w=' + w + amp + 'h=' + h + amp + 'c=' + c + amp + 'r=' + f + amp + 'u='+ u + amp + 'fs=' + fs + amp + 'b=' + b + amp + 'x=' + x;
Copy linkTweet thisAlerts:
@memarkMay 03.2005 — Or put the script in a separate file and use <script src="filename.js"></script>.
Copy linkTweet thisAlerts:
@nzakasMay 04.2005 — The best way to deal with this is to put your code in an external file. XHTML is XML, meaning that any code inside of the <script>...</script> tags must be escaped or surrounded in a CDATA section. Since IE doesn't recognize CDATA, your best bet is to put the code in an external file.
Copy linkTweet thisAlerts:
@CharlesMay 04.2005 — Contrary to what you may have read or heard, XHTML is actually really different from HTML.

In HTML the Document Type Declaration (DTD) we find &lt;!ENTITY % Script "CDATA" -- script expression --&gt;
...
&lt;!ELEMENT SCRIPT - - %Script; -- script statements --&gt;
From this we, and the browser, know that the contents of the SCRIPT element should be treated as character data (CDATA). CDATA gets left as it is, for the most part. HTML also has a optional tags for some elements so we need the DTD to do any error checking at all.

But the whole idea behind XHTML is to allow for some error checking without the DTD. An XHTML document, like any XML document, can be said to be "well formed" without a DTD. But this means that XHTML has no optional tags, and it means that the SCRIPT element has to take Parsed Character Data (PCDATA). This means that as the browser's parser builds the document tree every time it hits a "<" character in the SCRIPT element it thinks that an element is beginning. ">" and "&" characters are likewise a problem.

There are several solutions.

The first, and best, is to simply use HTML 4.01 Strict. There are few good reasons for using XHTML and one ought never to use it unless one really knows what one is doing. If you are still reading this and this is new to you, then you are not somebody who really knows what they are doing.

On the other hand, one could replace those troublesome characters with entities: "&lt;" for "<", "&gt;" for ">" and "&amp;" for "&".

Or one could use the XML designation for CDATA:&lt;script type="text/javascript"&gt;
&lt;![CDATA[
alert ('Hello, World!');
]]&gt;
&lt;/script&gt;
Note, however, that in XHTML browsers are permitted to completely ignore anything that is found inside a comment. Do not use comments to hide script from old browsers!!!

The W3C suggests that you just use external scripts.


One more, really important point. XHTML browsers are supposed to not support [font=monospace]document.write[/font] ( http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite & http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ )

It really is best to just use HTML 4.01 Strict.
×

Success!

Help @Cetalfio spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...