[Bug 2669] URL encoding in JavaScript/PHP

http://www.w3.org/Bugs/Public/show_bug.cgi?id=2669

           Summary: URL encoding in JavaScript/PHP
           Product: Validator
           Version: HEAD
          Platform: PC
               URL: http://bungalan.nl/test.php
        OS/Version: Windows 2000
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Parser
        AssignedTo: link@pobox.com
        ReportedBy: info@megaman.nl
         QAContact: www-validator-cvs@w3.org


Hello,

the following has kept me busy for some time. While validating pages I've
noticed a problem with URLs in JavaScript. I'm supposed to encode all & into
&amp; in a page and in <A> tags. The validator requires me to encode strings in
JavaScript also. But this will cause trouble for PHP script.

I've created a sample (/simple) page that demonstrates the issue:
----------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Validator test</title>
<script language="JavaScript" type="text/javascript">
function openLink() {
window.location.href = '<?php echo $_SERVER['SCRIPT_NAME']; ?>?a=1&amp;b=2';
}
</script>
</head>
<body>
<a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>?a=1&amp;b=2">open via
link</a><br /><br />
<form method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>?a=1&amp;b=2">
<input type="submit" value="open via form" /><br /><br />
</form>
<input type="button" value="open via jscript" onclick="openLink()" /><br /><br />
<pre><?php
// show submitted vars
var_dump($_REQUEST);
?></pre>
</body>
</html>
----------------------------------------------------------------------

The link and form methods will correctly output:
array(2) {
  ["a"]=>
  string(1) "1"
  ["b"]=>
  string(1) "2"
}

The JavaScript method however will output:
array(2) {
  ["a"]=>
  string(1) "1"
  ["amp;b"]=>
  string(1) "2"
}

So with the JS method it seems the URL isn't decoded. I'm wondering what the
problem is...

a) It's not required to encode JS strings, and the validator incorrectly checks
them;
b) PHP isn't decoding for some reason;
c) the browser doesn't handle the string as it should (tested with latest MS IE
and Mozilla FF);
d) I'm all wrong and there is a better way to do what I want.

Would be nice to hear what other people think.


Regards, Willem

Received on Friday, 6 January 2006 14:45:26 UTC