homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

    
'&' or '&' in URLs?
html vs javascript
Cook




msg:576018
 1:33 am on Oct 15, 2004 (gmt 0)

Hi,

W3C warns against variable names in URLs that could be interpreted as html entities, like:

<A HREF="script.php?var=my_var&amp=my_amp">

as &amp might be replaced by forgiving browsers (those that do not require html entities to end with ;) with & resulting in the URL looking like:

script.php?var=my_var&=my_amp

So it is recommended to write such an anchor this way:

<A HREF="script.php?var=my_var&amp;amp=my_amp">

Having said that, what about the following:

<A HREF="javascript:window.open('script.php?var=my_var&amp=my_amp', '', 'width=500,height=250')">

I would assume that as soon as browsers see javascript: in the HREF attribute, the javascript interpreter would take over and just '&' is ok. I tried both '&' and '&amp;' and both work equally well (in IE at least).

So well it's cool it works, but I'd like to understand what's actually going on and who's at the controls, html or javascript?

Any experience with that?

Thanks!

Cheers,
Cook

 

kaled




msg:576019
 2:40 am on Oct 15, 2004 (gmt 0)

I would think HTML is in control, therefore &amp; is required.

To convince yourself, try something like this

href="javascript:alert('&amp; hello');"

I'm pretty sure it will display '& hello';

Kaled.

Cook




msg:576020
 2:47 am on Oct 15, 2004 (gmt 0)

Hi kaled,

Thanks for this, you're right that's what is displayed. So one can assume that html is in control for parsing text in tag attributes, eventhough it actually is some javascript.

How about text between <script></script> tags? Oh, is it why javascript statements are enclosed with <!-- and --> as in:

<script ...>
<!--
javascript code
-->
</script>

Cheers,
Cook

Cook




msg:576021
 2:53 am on Oct 15, 2004 (gmt 0)

I answer my own question:

<html>
<head><script>
function test()
{
alert('&amp; hello');
}
</script></head>
<body>
<a href="javascript:alert('&amp; hello')">test me</a>
<a href="javascript:test()">no, test me</a>
</body>
</html>

The first link display '& hello', the second, '&amp; hello'. So html stays out of what's within <script></script>.

Cheers,
Cook

kaled




msg:576022
 11:00 am on Oct 15, 2004 (gmt 0)

One thing to be aware of

If javascript is embedded in an html file the character string combination '</' may be treated as '</SCRIPT>' wherever it occurs. Therefore this should be written as '<\/'

Kaled.

fathom




msg:576023
 11:54 am on Oct 15, 2004 (gmt 0)

<A HREF="script.php?var=my_var&amp=my_amp">

don't forget that &#061; is = in unicode as well.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved