Forum Moderators: open

Message Too Old, No Replies

XHTML javascript problem

         

music_man

10:41 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Hi all

I am having trouble validating this javascript code to XHTML strict.

<script type="text/javascript">if ((screen.width==1024)&(screen.height==768)){document.write ("<link rel='stylesheet' type='text/css' href='<?= $path?>/inc/css/1024x768.css' />");}if ((screen.width==1280)&(screen.height==1024)){document.write ("<link rel='stylesheet' type='text/css' href='<?= $path?>/inc/css/1280x1024.css' />'");}</script>

<?=path?> is the webpage path.

Any ideas please?

Thank you

Bernard Marx

1:17 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Wrap any on-page script/CSS in CDATA tags

<script type="text/javascript">
// <[CDATA[

...script...

// ]]>
</script>

music_man

10:36 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Thank you for your reply.

<script type="text/javascript">

// <[CDATA[

if ((screen.width==1024)&(screen.height==768)){document.write ("<link rel='stylesheet' type='text/css' href='<?= $path?>/inc/css/1024x768.css' />");}if ((screen.width==1280)&(screen.height==1024)){document.write ("<link rel='stylesheet' type='text/css' href='<?= $path?>/inc/css/1280x1024.css' />'");}

// ]]>

</script>

Thank you

[edited by: jatar_k at 2:22 am (utc) on Oct. 7, 2005]
[edit reason] no urls thanks [/edit]

encyclo

12:16 am on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem revolves around the fact that the HTML validator is attempting to parse the Javascript - the errors are actually bogus because the content is not actually (X)HTML.

The best way is to stick to external scripts, placing the Javascript in a separate file and calling it from the page.

music_man

12:32 am on Oct 7, 2005 (gmt 0)

10+ Year Member



Yeah good idea. However I have the problem with <?=$path?> which I use to link to the correct style sheets. Maybe I'll have to hard code it and have it as external.

Thanks

Bernard Marx

7:15 am on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?=$path?>

Is that PHP? That shouldn't be appearing in the final output anyway, so validation of that isn't an issue.

music_man

11:56 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



If I store the script in a .js file then it wont parse the php script.

No matter, I have resolved this problem.

Thank you for your replies.

MindlessXD

2:35 am on Oct 9, 2005 (gmt 0)

10+ Year Member



make your javascript a php script itself e.g.
<script type="text/javascript" scr="screenmodes_js.php"></script>

screenmodes_js.php would be

<?php header("Content-Type: text/javascript");
$path = "/path/to";
if ((screen.width==1024)&(screen.height==768)){document.write ("<link rel='stylesheet' type='text/css' href='<?= $path?>/inc/css/1024x768.css' />");}if ((screen.width==1280)&(screen.height==1024)){document.write ("<link rel='stylesheet' type='text/css' href='<?= $path?>/inc/css/1280x1024.css' />'");}