homepage Welcome to WebmasterWorld Guest from 54.161.161.255
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
escaping ampersands in javascript
simple for anyone that knows javascript :-)
drnoble

10+ Year Member



 
Msg#: 353 posted 12:51 pm on Apr 9, 2003 (gmt 0)

Hi, I have a small script in a sidebar I have developed for Mozilla. I am trying to get it to validate as xhtml 1 transitional

the script is

function member(){
var m;m=prompt('Member name:');
if(m!=null){_content.location.href="member.php?action=getinfo&username="+m;}
};

and the validator does not like the unescaped ampersand... how do i go about escaping it.

a quick googling has not brought up anything i understand, so thanks for your help, Donald

 

RonPK

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 353 posted 12:59 pm on Apr 9, 2003 (gmt 0)

Try &

Good luck!

HocusPocus

10+ Year Member



 
Msg#: 353 posted 1:04 pm on Apr 9, 2003 (gmt 0)

getinfo%26username

?

drnoble

10+ Year Member



 
Msg#: 353 posted 1:18 pm on Apr 9, 2003 (gmt 0)

unfortunately niether of those work, as the & has to be as an & in the url, not & or %26, as it is submitting to a php page.

i think the solution might be to use JS to insert the character code into the string, but that is what i am not sure how to do.

thanks anyway, donald

Alternative Future

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 353 posted 1:20 pm on Apr 9, 2003 (gmt 0)

Hi,

Have you tried replacing the double quotes for single ones?

i.e. content.location.href='member.php?action=getinfo&username='+m;}

[added]
I thought this might be of interest [w3.org]
*OR*
Markup Languages Coding Guidelines for Mozilla.org [mozilla.org]
[/added]

-gs

RonPK

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 353 posted 3:47 pm on Apr 9, 2003 (gmt 0)

drnoble, & works fine with my IE6 and Opera7 and is the thing to do according to all the documentation I've seen. One should not feed it to the browsers address bar, though.

Anyway, I've never understood why (x)html validators try to interpret the code inside <script>-tags. None of their business, imho.

tedster

WebmasterWorld Senior Member tedster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 353 posted 4:34 pm on Apr 9, 2003 (gmt 0)

Maybe I'm missing something, but why not define the function in an external .js file? The validator won't see it at all.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 353 posted 6:33 pm on Apr 9, 2003 (gmt 0)

You should always use &amp; for this. The browser should unescape the ampersand and send just an & to the server in the request, and the browser should display just an & on screen. However, in the source code you should be using the full &amp; notation.

As noted above, using external javascript is even better. You link to the .js file with this:
<script type="text/javascript" language="javascript" src="/path/file.js"></script>

drnoble

10+ Year Member



 
Msg#: 353 posted 4:44 pm on Apr 11, 2003 (gmt 0)

I could put it in an external file, but for a one line script on a sidebar it does not seem worth it.

the solution is either to use html comments (invalid) or to mark it as CDATA

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

the answer: [webmasterworld.com...]
which links to: [w3.org...]

BjarneDM

10+ Year Member



 
Msg#: 353 posted 6:31 pm on Apr 11, 2003 (gmt 0)

RonPK :
XML-parsers are *required* to look at everything unless specifically told not to.

DrNoble :
the correct form is -
<script type="text/javascript">
// <![CDATA[
script here
// ]]>
</script>

[devedge.netscape.com...]

DrNoble :
I've solved the escape-the-ampersand-problem by going over to ';' instead.
In php you have to change two settings -
arg_separator.output = ";"
arg_separator.input = ";"

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
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