/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Code does not validate

Hi. I found the following code that seems to work great to load a random .css file. Problem is, the code does not validate properly with 1.0 the transitional xhtml specs.

It seems to be the backslashes and ‘+’ signs that are flagging as non-complaint. Mostly from this line..

[CODE]
document.write(‘<link rel=”stylesheet” href=”‘ +randstylesheet+'” type=”text/css”>’);
[/CODE]

Here is the code..

[CODE]
<script type=”text/javascript”>
function randomcss(min,max)
{
var randstylesheet = “indextest.css”;
var randnumber = (Math.round(Math.random()*(max-min)))+min;

switch(randnumber)
{
case 0: randstylesheet = “/indextest.css”;break
case 1: randstylesheet = “/site.css”;break
}

document.write(‘<link rel=”stylesheet” href=”‘ +randstylesheet+'” type=”text/css”>’);
}
randomcss(0,8);
</script>
[/CODE]

Any ideas?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 12.2006 — It seems to be the backslashes and '+' signs that are flagging as non-complaint.[/QUOTE]
Neither all those backslashes nor the plus-signs are needed:

document.write('<link rel="stylesheet" href="', randstylesheet, '" type="text/css">');
Copy linkTweet thisAlerts:
@mrhooJun 12.2006 — Don't forget to close the link element to validate as xhtml:

<link rel="stylesheet" href="', randstylesheet, '" type="text/css" [B]/>[/B]
Copy linkTweet thisAlerts:
@supersteve3dauthorJun 12.2006 — Thanks for the help. Its better but still doesnt validate.

Apologies if this is drifting more into html/xhtml than javascript.


[CODE]Error Line 26 column 88: document type does not allow element "link" here.

...randstylesheet, '" type="text/css" />');

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).
[/CODE]
Copy linkTweet thisAlerts:
@UltimaterJun 12.2006 — Forgetting to use CDATA:
<i>
</i>&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
[color=blue]INSERT CODE HERE[/color]
//]]&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@supersteve3dauthorJun 12.2006 — Thank you Ultimater! That worked.
×

Success!

Help @supersteve3d 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...