By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

javascript:pick doesn't work with the ' character

P: n/a
Hi,
If I have the ' character within the javascript:pick command it doesn't
work. Is there some sort of way of escaping these characters like in
server side languages.

function pick(symbol) {
if (window.opener && !window.opener.closed)
window.opener.document.create.text1.value = symbol;
window.close();
}

<A HREF="javascript:pick('This doesn't work')">This doesn't work</A>

It works fine as long as there is no ' in the pick bit. I need to be
able to accept any characters in there as it's created by the users.

Any ideas?

Thanks

Gary
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Wed, 25 Feb 2004 15:34:33 +0000, Gary Mayor <ga**@abertron.co.uk> wrote:
If I have the ' character within the javascript:pick command it doesn't
work. Is there some sort of way of escaping these characters like in
server side languages.

function pick(symbol) {
if (window.opener && !window.opener.closed)
window.opener.document.create.text1.value = symbol;
window.close();
}

<A HREF="javascript:pick('This doesn't work')">This doesn't work</A>

It works fine as long as there is no ' in the pick bit. I need to be
able to accept any characters in there as it's created by the users.

Any ideas?


Read the FAQ, specifically:

http://jibbering.com/faq/#FAQ4_24

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #2

P: n/a
Michael Winter wrote:
On Wed, 25 Feb 2004 15:34:33 +0000, Gary Mayor <ga**@abertron.co.uk> wrote:
If I have the ' character within the javascript:pick command it
doesn't work. Is there some sort of way of escaping these characters
like in server side languages.

function pick(symbol) {
if (window.opener && !window.opener.closed)
window.opener.document.create.text1.value = symbol;
window.close();
}

<A HREF="javascript:pick('This doesn't work')">This doesn't work</A>

It works fine as long as there is no ' in the pick bit. I need to be
able to accept any characters in there as it's created by the users.

Any ideas?

Read the FAQ, specifically:

http://jibbering.com/faq/#FAQ4_24

Mike


Hi,
Thanks for that i guess that means i have to do it using a function. How
do you escape characters as a string in javascript. In perl I do it like
this

$a = qq{Some Stuff};

does javascripts have a similar function that will work with my problem.

Thanks

Gary
Jul 20 '05 #3

P: n/a
On Wed, 25 Feb 2004 16:25:29 +0000, Gary Mayor <ga**@abertron.co.uk> wrote:

[snip]
How do you escape characters as a string in javascript. In perl I
do it like this

$a = qq{Some Stuff};

does javascripts have a similar function that will work with my problem.


In literals, you use a backslash. In variables, it depends why you want to
escape a string.

The escape() and unescape() functions might be appropriate, but they are
designed for escaping strings before being place in a URL. As such, some
of the characters you might want escaped won't be, and some of the
characters you don't want escaped will be. They also introduce some hassle
with spaces: spaces are encoded as plus symbols (+), but plus symbols in
the original string are left intact. When unescaped, all plus symbols
(including those that were spaces) still remain in the string. If the
original string did contain plus symbols, you won't be able to perform a
simple search and replace.

Experiment. If they suit your needs, then fine. If not, then you'll have
to explain in more detail.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #4

P: n/a
The original problem is that javascript sees:
pick('This doesn't work')

Michael's suggestion would give:
pick(unescape('This%20doesn%27t%20work'))
In article <op**************@news-text.blueyonder.co.uk>,
Michael Winter <M.******@blueyonder.co.invalid> wrote:
On Wed, 25 Feb 2004 16:25:29 +0000, Gary Mayor <ga**@abertron.co.uk> wrote:

[snip]
How do you escape characters as a string in javascript. In perl I
do it like this

$a = qq{Some Stuff};

does javascripts have a similar function that will work with my problem.


In literals, you use a backslash. In variables, it depends why you want to
escape a string.

The escape() and unescape() functions might be appropriate, but they are
designed for escaping strings before being place in a URL. As such, some
of the characters you might want escaped won't be, and some of the
characters you don't want escaped will be. They also introduce some hassle
with spaces: spaces are encoded as plus symbols (+), but plus symbols in
the original string are left intact.


The escape and unescape functions are used in common cookie routines.
I'd hope the round trip would be OK. Has this changed over time or in
different browsers?

In my two tests, I saw the space character changed to a %20. The plus
character wasn't changed.

Here is my test file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=windows-1252">
<title>Remember by cookie</title>
<script type="text/javascript">
function beforePick(inData) {

pick(escape(inData));
pick(unescape(escape(inData)));
}

function pick(symbol) {
alert("symbol = " + symbol);
}
</SCRIPT>
<BODY>
<p>Let's try some escapes in the MacOS of Netscape 7.1.and IE 5.2.</p>

<script type="text/javascript">
var all = "I hope this quote doesn't crumble.";
var moreAll = "A bunch of troublesome characters: <br> <\/br> ;
+_\\{}[]";
document.write("<p>");
document.write("escape(all) = " + escape(all) +"<\/br>");
document.write("escape(moreAll) = " + escape(moreAll) +"<\/br>");
document.write("</p>");
var data = 'This doesn\'t work';
beforePick(data)
document.write("escape(data) = " + escape(data));
</SCRIPT>
</body>
</html>
Jul 20 '05 #5

P: n/a
Robert <c6**************************@posting.google.com > wrote:
[top-post fixed]

Pleas do not top-post to comp.lang.javascript. The group FAQ outlines
acceptable posting style in section 2.3 paragraph 5 and references the
applicable standard.
Michael Winter wrote:
Gary Mayor wrote: <snip>
does javascripts have a similar function that will
work with my problem.
In literals, you use a backslash. ...

<snip>
The original problem is that javascript sees:
pick('This doesn't work')

Michael's suggestion would give:
pick(unescape('This%20doesn%27t%20work'))


In the context of the OP's original code Michael appears to me to be
endorsing a backslash escape:-

onclick="pick('This doesn\'t work');return false;"

The rest of his post seems to be additional information on some of the
escaping functions available in javascript for comparison with perl's.

Richard.
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.