Forum Moderators: open

Message Too Old, No Replies

& sign in Javascript not xhtml validated

         

km603

2:46 am on Nov 24, 2008 (gmt 0)

10+ Year Member



Hi.

I wanted to have my page xhtml validated.

But one of the line fomr javascript caused an error.

the line is:

document.location.href = "userpanel.php?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";

I know I need to replace & with &

however, &amp was get rendered as &.

I tried

document.location.href = "userpanel.php?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";

On my browser, the url became:

userpanel.php?day=23&month=11&year=2008&mode=dayview

so it was not working.

Can anyone tell me how to fix it?

Thanks.

lavazza

4:21 am on Nov 24, 2008 (gmt 0)

10+ Year Member



One effective workaround:

import the code form an external file (which the w3c validator will not read)

e.g.
<head>
<snip/>
<script type="text/javascript" src="http://www.example.com/scripts/myUglyAmpersandWorkaroundFile.js"></script>
</snip>
</head>

:)

km603

4:28 am on Nov 24, 2008 (gmt 0)

10+ Year Member



i also thought of that too.

but i would need 3 different JS files for :

"userpanel.php?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";

"month.php?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";

"year?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";


I guess this is the only solution.

Thanks for your reply.

lavazza

4:48 am on Nov 24, 2008 (gmt 0)

10+ Year Member



but i would need 3 different JS files

Not necessarily...

One file with three vars ought to do it

e.g. (I've used single quotes instead of double quotes)

var variablename01 = 'userpanel.php?day=' + day + '&month=' + month + '&year=' + year + '&mode=dayview';

var variablename02 = 'month.php?day=' + day + '&month=' + month + '&year=' + year + '&mode=dayview';

var variablename03 = 'year?day=' + day + '&month=' + month + '&year=' + year + '&mode=dayview';

and then... in the <body>

<p>
Lorem ipsum dolor sit amet,
<script type="text/javascript">
document.write(variablename02);
</script>
consectetuer adipiscing
<script type="text/javascript">
document.write(variablename01);
</script>
elit. Donec in purus a
<script type="text/javascript">
document.write(variablename03);
</script>
dui venenatis laoreet.
</p>

km603

4:55 am on Nov 24, 2008 (gmt 0)

10+ Year Member



oh ya. nice.

Thanks a lot.

astupidname

7:00 am on Nov 24, 2008 (gmt 0)

10+ Year Member



If you need to have javascript validate in xhtml, it should be wrapped in CDATA tags which are commented out. While it is best to have javascript be external, there are those situations sometimes where it is a small amount you don't want to use an external file for. For example, this will validate:

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

var item1 = "userpanel.php?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";
var item2 = "month.php?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";
var item3 = "year?day=" + day + "&month=" + month + "&year=" + year + "&mode=dayview";
document.location.href = "http://www.example.com/" + item1;

/*]]>*/
</script>