473,394 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to escape apostrophe

Hello All:

I'm having a whale of a problem with the following code. When the words
beginning with sugg contain an escaped single-quote (\' ==> back-slash
followed by quote) the script causes an error. How can I preserve the
single quote. Please review the code below and let me know. Many
thanks!

function clicker( but_id, sugg0, sugg1, sugg2, sugg3, sugg4, sugg5,
copy_type, p1) {
var suggx;
if (p1 ==1) suggx=sugg1;
else if (p1 ==2) suggx=sugg2;
else if (p1 ==3) suggx=sugg3;
else if (p1==4) suggx=sugg4;
else suggx=sugg5;

eval("document.forms[0]." + but_id + ".value='" + suggx +
"'");

eval("document.forms[0]." + but_id +
".style.backgroundColor='#ffffff'");

eval("document.forms[0]." + but_id +
".style.color='#000000'");
//alert("in clicker");

var cont;
if (copy_type.indexOf("HTML_COPY") > -1) {
cont =document.forms[0].HTML_COPY.value;
//alert("conthtml=" + cont);
//alert("sugg0=" + sugg0);
var
txt="document.forms[0].HTML_COPY.value=cont.replace(/" + sugg0 + "/" +
replaceOpts + ",'" + suggx + "')";
eval(txt);
//alert( document.forms[0].HTML_COPY.value);
} else {
cont =document.forms[0].AOL_COPY.value;
//alert("contaol=" + cont);
//alert("sugg0=" + sugg0 + " suggx= " + suggx + "
replaceOpts=" + replaceOpts);
var
txt="document.forms[0].AOL_COPY.value=cont.replace(/" + sugg0 + "/" +
replaceOpts + ",'" + suggx + "')";
eval(txt);
//alert( document.forms[0].AOL_COPY.value);
//alert( document.forms[0].AOL_COPY.value);
}
return true;
}
function showSugg(but_id, sugg0, sugg1, sugg2, sugg3, sugg4, sugg5,
copy_type) {

if (sugg0.indexOf("'") > -1) sugg0=replaceAll(sugg0, "'",
"\'");
if (sugg1.indexOf("'") > -1) sugg1=replaceAll(sugg1, "'",
"\'");
if (sugg2.indexOf("'") > -1) sugg2=replaceAll(sugg2, "'",
"\'");
if (sugg3.indexOf("'") > -1) sugg3=replaceAll(sugg3, "'",
"\'");
if (sugg4.indexOf("'") > -1) sugg4=replaceAll(sugg4, "'",
"\'");
if (sugg5.indexOf("'") > -1) sugg5=replaceAll(sugg5, "'",
"\'");

display=window.open('','_blank','menubar=0,locatio n=no,status=no,directories=no,toolbar=no,scrollbar s=no,height=220,width=250')
var screen_height = window.screen.availHeight;
var screen_width = window.screen.availWidth;
var left_point = parseInt(screen_width / 2);
var top_point = parseInt(screen_height/2);
display.moveTo(left_point, top_point);
display.focus();

//alert("display elements" + display.document.elements);
alert("sugg1" + sugg1 + "2" + sugg2 + "3" + sugg3 + "4" +
sugg4 + "5" + sugg5);

if (sugg1)
var message="<HTML><BODY><font face='verdana, arial,
helvetica, san-serif' size='2'><form><table>";
message += "<tr><td>" + sugg1 + "</td> <td><input
type=checkbox name=mycheck1 value=\"" + sugg1 +
"\" onClick=\'self.opener.clicker(\"" + but_id + "\",\"" +
sugg0 + "\",\""
+ sugg1 + "\",\"" + sugg2 + "\",\"" + sugg3 + "\",\"" + sugg4
+ "\",\"" + sugg5 +
"\",\"" + copy_type + "\", 1);"
+ "document.forms[0].mycheck2.checked=false;"
+
"document.forms[0].mycheck3.checked=false;document.forms[0].mycheck4.checked=false;"
+ "document.forms[0].mycheck5.checked=false;"
+ "\'></td></tr>";
if (sugg2)
message += "<tr><td>" +sugg2 + " </td><td><input type=checkbox
name=mycheck2 value=\"" + sugg2 +
"\" onClick=\'self.opener.clicker(\"" + but_id + "\",\"" +
sugg0 + "\",\""
+ sugg1 + "\",\"" + sugg2 + "\",\"" + sugg3 + "\",\"" + sugg4
+ "\",\"" + sugg5 +
"\",\"" + copy_type + "\", 2);"
+ "document.forms[0].mycheck1.checked=false;"
+
"document.forms[0].mycheck3.checked=false;document.forms[0].mycheck4.checked=false;"
+ "document.forms[0].mycheck5.checked=false;"
+ "\'></td></tr>";

if (sugg3)
message += "<tr><td>" +sugg3 + " </td><td><input type=checkbox
name=mycheck3 value=\"" + sugg3 +
"\" onClick=\'self.opener.clicker(\"" + but_id + "\",\"" +
sugg0 + "\",\""
+ sugg1 + "\",\""+ sugg2 + "\",\"" + sugg3 + "\",\"" + sugg4 +
"\",\"" + sugg5 +
"\",\"" + copy_type + "\", 3);"
+ "document.forms[0].mycheck2.checked=false;"
+
"document.forms[0].mycheck1.checked=false;document.forms[0].mycheck4.checked=false;"
+ "document.forms[0].mycheck5.checked=false;"
+ "\'></td></tr>";

if (sugg4)
message += "<tr><td>" +sugg4 + " </td><td><input
type=checkbox name=mycheck4 value=\"" + sugg4 +
"\" onClick=\'self.opener.clicker(\"" + but_id + "\",\""+
sugg0 + "\",\""
+ sugg1 + "\",\""+ sugg2 + "\",\"" + sugg3 + "\",\"" + sugg4 +
"\",\"" + sugg5 +
"\",\"" + copy_type + "\", 4);"
+ "document.forms[0].mycheck2.checked=false;"
+
"document.forms[0].mycheck3.checked=false;document.forms[0].mycheck1.checked=false;"
+ "document.forms[0].mycheck5.checked=false;"
+ "\'></td></tr>";

if (sugg5)
message += "<tr><td>" +sugg5 + " </td><td><input
type=checkbox name=mycheck5 value=\"" + sugg5 +
"\" onClick=\'self.opener.clicker(\"" + but_id + "\",\"" +
sugg0 + "\",\""
+ sugg1 + "\",\"" + sugg2 + "\",\"" + sugg3 + "\",\"" + sugg4
+ "\",\"" + sugg5
+ "\",\"" + copy_type + "\", 5);"
+ "document.forms[0].mycheck2.checked=false;"
+
"document.forms[0].mycheck3.checked=false;document.forms[0].mycheck4.checked=false;"
+ "document.forms[0].mycheck1.checked=false;"
+ "\'></td></tr>";

message += "<tr><td> <input type=\"button\" class=\"btn\"
name=\"Replace\" value=\"Replace\"
onclick=\"self.opener.replaceOpts=\'i\'\"></td>";
message += "<td> <input type=\"button\" class=\"btn\"
name=\"Replace All\" value=\"Replace All\"
onclick=\"self.opener.replaceOpts=\'ig\'\"></td>";
message += "<td> <input type=\"button\" class=\"btn\"
name=\"Done\" value=\"Done\"
onclick=\"self.opener.display.close()\"></td></tr>";
message += "</table></font>";
alert(message);

display.document.open();
display.document.write(message + "</BODY></HTML>");
display.document.close();

}

Jul 23 '05 #1
15 7945
wrote on 11 jul 2005 in comp.lang.javascript:
eval("document.forms[0]." + but_id + ".value='" + suggx +
"'");

eval is evil!

Try:

document.forms[0][but_id].value = suggx

=============================
var suggx;
if (p1 ==1) suggx=sugg1;
else if (p1 ==2) suggx=sugg2;
else if (p1 ==3) suggx=sugg3;
else if (p1==4) suggx=sugg4;
else suggx=sugg5;


a variation, easier with long lists:

var suggAr = [0, sugg1, sugg2, sugg3, sugg4]
var suggx=sugg5;
if (p1<5 && p1>0) suggx=suggAr[p1];

Not tested

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #2
ASM
so***********@yahoo.com wrote:
Hello All:

I'm having a whale of a problem with the following code. When the words
beginning with sugg contain an escaped single-quote (\' ==> back-slash
followed by quote) the script causes an error. How can I preserve the
single quote. Please review the code below and let me know. Many
thanks!

function clicker( but_id, sugg0, sugg1, sugg2, sugg3, sugg4, sugg5,
copy_type, p1) {
var suggx;
if (p1 ==1) suggx=sugg1;
else if (p1 ==2) suggx=sugg2;
else if (p1 ==3) suggx=sugg3;
else if (p1==4) suggx=sugg4;
else suggx=sugg5;

eval("document.forms[0]." + but_id + ".value='" + suggx +
"'");
What do you expect to do with this eval ?

Did
document.forms[0][but_id].value = suggx;
or
document.forms[0].elements[but_id].value = suggx;
or
document.forms[0].but_id.value = suggx;
not work ?
eval("document.forms[0]." + but_id +
".style.backgroundColor='#ffffff'");
document.forms[0].but_id.style.backgroundColor='#ffffff'
eval("document.forms[0]." + but_id +
".style.color='#000000'");
//alert("in clicker");

var cont;
if (copy_type.indexOf("HTML_COPY") > -1) {
cont =document.forms[0].HTML_COPY.value;
//alert("conthtml=" + cont);
//alert("sugg0=" + sugg0);
var
txt="document.forms[0].HTML_COPY.value=cont.replace(/" + sugg0 + "/" +
replaceOpts + ",'" + suggx + "')";
eval(txt);


from where comes "replaceOpts" ?

txt=document.forms[0].HTML_COPY.value=cont.replace(sugg0/replaceOpts,suggx);
or without using 'cont' :
with(document.forms[0].HTML_COPY){txt=value=value.replace(sugg0/replaceOpts,suggx);}

perhaps is it that
with(document.forms[0].HTML_COPY) {
txt = value = value.replace(sugg0+'/replaceOpts',suggx);
}
what you want to do ?
--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #3
ASM wrote:
so***********@yahoo.com wrote:
Hello All:

I'm having a whale of a problem with the following code. When the words
beginning with sugg contain an escaped single-quote (\' ==> back-slash
followed by quote) the script causes an error. How can I preserve the
single quote. Please review the code below and let me know. Many
thanks!

function clicker( but_id, sugg0, sugg1, sugg2, sugg3, sugg4, sugg5,
copy_type, p1) {
var suggx;
if (p1 ==1) suggx=sugg1;
else if (p1 ==2) suggx=sugg2;
else if (p1 ==3) suggx=sugg3;
else if (p1==4) suggx=sugg4;
else suggx=sugg5;

eval("document.forms[0]." + but_id + ".value='" + suggx +
"'");
What do you expect to do with this eval ?

Did
document.forms[0][but_id].value = suggx;
or
document.forms[0].elements[but_id].value = suggx;
or
document.forms[0].but_id.value = suggx;
not work ?


It is the last one. The eval works fine. I tried substituting single
quotes in the sugg variables with the string "AQUOTE" and it works
fine. The moment I use single quotes, albeit escaped, the code breaks
down.


eval("document.forms[0]." + but_id +
".style.backgroundColor='#ffffff'");
document.forms[0].but_id.style.backgroundColor='#ffffff'

eval("document.forms[0]." + but_id +
".style.color='#000000'");
//alert("in clicker");

var cont;
if (copy_type.indexOf("HTML_COPY") > -1) {
cont =document.forms[0].HTML_COPY.value;
//alert("conthtml=" + cont);
//alert("sugg0=" + sugg0);
var
txt="document.forms[0].HTML_COPY.value=cont.replace(/" + sugg0 + "/" +
replaceOpts + ",'" + suggx + "')";
eval(txt);


from where comes "replaceOpts" ?


replaceOpts is a global. Also replaceAll is simple helper function

function replaceAll( str, from, to ) {
var idx = str.indexOf( from );
while ( idx > -1 ) {
str = str.replace( from, to );
idx = str.indexOf( from );
}
return str;

} txt=document.forms[0].HTML_COPY.value=cont.replace(sugg0/replaceOpts,suggx);
or without using 'cont' :
with(document.forms[0].HTML_COPY){txt=value=value.replace(sugg0/replaceOpts,suggx);}

perhaps is it that
with(document.forms[0].HTML_COPY) {
txt = value = value.replace(sugg0+'/replaceOpts',suggx);
}
what you want to do ?


Thanks for dissecting my code. I however am still left with the problem.

Jul 23 '05 #4
Lee
so***********@yahoo.com said:
It is the last one. The eval works fine. I tried substituting single
quotes in the sugg variables with the string "AQUOTE" and it works
fine. The moment I use single quotes, albeit escaped, the code breaks
down.


Post your code as corrected to escape the quote and to avoid eval().
Nobody wants to help debug code that uses eval() when it isn't
necessary.

Jul 23 '05 #5
On 11/07/2005 14:24, so***********@yahoo.com wrote:
I'm having a whale of a problem with the following code.


Indeed you are, and as others have said, the problem is because you are
using the eval function. Though it can be 'fixed', the code should be
rewritten.

The remainder of this post contains such a rewrite, but as I have no
idea how you'd end up using it, it could be just a broken. If so, post a
URL to the page in question.

var display;

function clicker(id, sugg0, sugg1, sugg2, sugg3,
sugg4, sugg5, type, p)
{
var sugg = arguments[p + 1],
controls = document.forms[0].elements,
elem;

elem = controls[id];
elem.value = sugg;
if(elem.style) {
elem.style.backgroundColor = 'white';
elem.style.color = 'black';
}

elem = controls[(-1 != type.indexOf('HTML_COPY'))
? 'HTML_COPY'
: 'AOL_COPY'
];
elem.value = elem.value.replace(new RegExp(sugg0, replaceOpts),
sugg);
return true;
}

function showSugg(id, sugg0, sugg1, sugg2,
sugg3, sugg4, sugg5, type)
{
var message = ['<html>',
'<head>',
'<title><\/title>',
'<style type="text/css">',
' body {font-family: Arial, sans-serif;}',
'<\/style>',
'<\/head>',
'<body>',
'<table>'];

for(var i = 1; i <= 5; ++i) {
if(arguments[i + 1]) {
message.push('<tr>',
'<td>' + arguments[i + 1] + '<\/td>',
'<td>',
'<input type=radio name=radio value="' + sugg1
+ '" onclick="self.opener.clicker(\'' + id
+ '\',\'' + sugg0 + '\',\'' + sugg1
+ '\',\'' + sugg2 + '\',\'' + sugg3
+ '\',\'' + sugg4 + '\',\'' + sugg5
+ '\',\'' + type + '\',' + i + ');">',
'<\/td>',
'<\/tr>');
}
}
message.push('<tr>',
'<td>',
'<input type="button" value="Replace" '
+ 'onclick="self.opener.replaceOpts='i';">',
'<\/td>',
'<td>',
'<input type="button" value="Replace All" '
+ 'onclick="self.opener.replaceOpts='ig';">',
'<\/td>',
'<td>',
'<input type="button" value="Done" '
+ 'onclick="self.opener.display.close();">',
'<\/td>',
'<\/tr>',
'<\/table>',
'<\/body>',
'<\/html>');

display = window.open('javascript:' + message.join('\n'));
}

if('function' != typeof Array.prototype.push) {
Array.prototype.push = function(v) {
var i = this.length >>> 0,
j = 0,
n = arguments.length;

while(n > j) {this[i++] = arguments[j++];}
return (this.length = i);
};
}

I've taken the liberty of cleaning things up, such as changing
checkboxes into what should /clearly/ be radio buttons.

As I said before, you haven't provided enough information to allow
testing, so that will have to be your responsibility.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 23 '05 #6
ASM
so***********@yahoo.com wrote:
ASM wrote:
so***********@yahoo.com wrote:
Hello All:

I'm having a whale of a problem with the following code. When the words
beginning with sugg contain an escaped single-quote (\' ==> back-slash
followed by quote) the script causes an error.
do you mean you call the function with one of its arguments written like
that ( \'foo' )
clicker('here',\'foo', ...
perhaps is it like that :
clicker('here','\'foo', ...
if it is, try :
clicker("here","'foo", ...

and ? on which moment (line) is the error ?
How can I preserve the
single quote. Please review the code below and let me know. Many
thanks!

function clicker( but_id, sugg0, sugg1, sugg2, sugg3, sugg4, sugg5,
copy_type, p1) {
var suggx;
if (p1 ==1) suggx=sugg1;
else if (p1 ==2) suggx=sugg2;
else if (p1 ==3) suggx=sugg3;
else if (p1==4) suggx=sugg4;
else suggx=sugg5;

eval("document.forms[0]." + but_id + ".value='" + suggx +
"'");
What do you expect to do with this eval ?

Did
document.forms[0][but_id].value = suggx;
or
document.forms[0].elements[but_id].value = suggx;
or
document.forms[0].but_id.value = suggx;
not work ?

It is the last one. The eval works fine.


still this line I would do
(but it's not necessary and is bad way to use eval)
if I well understood, it doesn't (with single quote)
further in your code
I tried substituting single
quotes in the sugg variables with the string "AQUOTE" and it works
fine. The moment I use single quotes, albeit escaped, the code breaks
down.
so ... did you try with one of what I'd suggest ?


eval("document.forms[0]." + but_id +
".style.backgroundColor='#ffffff'");
document.forms[0].but_id.style.backgroundColor='#ffffff'
eval("document.forms[0]." + but_id +
".style.color='#000000'");
//alert("in clicker");

var cont;
if (copy_type.indexOf("HTML_COPY") > -1) {
cont =document.forms[0].HTML_COPY.value;
//alert("conthtml=" + cont);
//alert("sugg0=" + sugg0);
var
txt="document.forms[0].HTML_COPY.value=cont.replace(/" + sugg0 + "/" +
replaceOpts + ",'" + suggx + "')";
eval(txt);


from where comes "replaceOpts" ?

replaceOpts is a global. Also replaceAll is simple helper function


I see upthere 'replace' and not 'replaceAll'
if it is replaceAll that is called right up
and :
- str is sugg0
- from is replaceOpts
- to is suggx
you could try writting :

with(document.forms[0].HTML_COPY) {
txt = value = value.replaceAll(sugg0,replaceOpts,suggx);
}

function replaceAll( str, from, to ) {
var idx = str.indexOf( from );
while ( idx > -1 ) {
str = str.replace( from, to );
idx = str.indexOf( from );
}
return str;

}
txt=document.forms[0].HTML_COPY.value=cont.replace(sugg0/replaceOpts,suggx);
or without using 'cont' :
with(document.forms[0].HTML_COPY){txt=value=value.replace(sugg0/replaceOpts,suggx);}

perhaps is it that
with(document.forms[0].HTML_COPY) {
txt = value = value.replace(sugg0+'/replaceOpts',suggx);
}
what you want to do ?

Thanks for dissecting my code.


I love that :-)

I however am still left with the problem.

if you could extract the moment of faill down it could be helping


--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #7
On 11/07/2005 17:17, Michael Winter wrote:

[snip]
'<input type="button" value="Replace" '
+ 'onclick="self.opener.replaceOpts='i';">',
[snip]
'<input type="button" value="Replace All" '
+ 'onclick="self.opener.replaceOpts='ig';">',


Seems I forgot to escape those nested quotes. The second in each snippet
above should contain =\'i\'; and =\'ig\'; respectively, not ='i'; and
='ig';.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 23 '05 #8
so***********@yahoo.com wrote:
I'm having a whale of a problem with the following code. eval("document.forms[0]." + but_id + ".value='" + suggx +
"'");


It is clear that you have received some extremely bad advice or
training. Did you learn to use eval from a badly written book, or did
you copy if from a badly written program?

http://www.JSLint.com
Jul 23 '05 #9


Michael Winter wrote:
On 11/07/2005 17:17, Michael Winter wrote:

[snip]
'<input type="button" value="Replace" '
+ 'onclick="self.opener.replaceOpts='i';">',


[snip]
'<input type="button" value="Replace All" '
+ 'onclick="self.opener.replaceOpts='ig';">',


Seems I forgot to escape those nested quotes. The second in each snippet
above should contain =\'i\'; and =\'ig\'; respectively, not ='i'; and
='ig';.

Mike


Many thanks for rewriting the code. I had to take out the back-slash
in the showSugg prefixed in the closing tags before the code could be
run in FireFox. Also FireFox gave this error:

Error: missing ) after argument list
Source Code:
showSugg('a1','isn't','USN','INS','ins','sin','ISB N','HTML_COPY')

Jul 23 '05 #10
ASM
so***********@yahoo.com wrote:

Also FireFox gave this error:

Error: missing ) after argument list
Source Code:
showSugg('a1','isn't','USN','INS','ins','sin','ISB N','HTML_COPY')

and :
showSugg(a1',"isn't",'USN','INS','ins','sin','ISBN ','HTML_COPY')
what that gives ?

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #11


ASM wrote:
so***********@yahoo.com wrote:

Also FireFox gave this error:

Error: missing ) after argument list
Source Code:
showSugg('a1','isn't','USN','INS','ins','sin','ISB N','HTML_COPY')

and :
showSugg(a1',"isn't",'USN','INS','ins','sin','ISBN ','HTML_COPY')
what that gives ?


Thanks for getting back. The code to wrap the apostrophe in the double
quotes is giving syntax error on FireFox.

Error: syntax error
Source Code:
showSugg('a2',

Jul 23 '05 #12
On 11/07/2005 17:17, Michael Winter wrote:

[snip]
elem = controls[(-1 != type.indexOf('HTML_COPY'))
? 'HTML_COPY'
: 'AOL_COPY'
];
I should have mentioned previously that if the type argument will always
be either 'HTML_COPY' or 'AOL_COPY', you can replace the above with:

elem = controls[type];

[snip]
function showSugg(id, sugg0, sugg1, sugg2,
sugg3, sugg4, sugg5, type)
{
[snip]
'<body>',
'<table>'];
'<body>',
'<table>'],
i;

/* The loop immediately below should perform the necessary
* entity replacements to make sure that any of the characters
* listed will not be inserted into the markup. Feel free to
* remove any that you know aren't a concern (if any).
*/
for(i = 2; i <= 6; ++i) {
arguments[i] = arguments[i]
? arguments[i].replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/&/g, '&amp;')
.replace(/'/g, '\\\'')
.replace(/"/g, '&quot;')
: '';
}
for(i = 2; i <= 6; ++i) {
if(arguments[i + 1]) {
message.push('<tr>',
'<td>' + arguments[i] + '<\/td>',
'<td>',
'<input type=radio name=radio value="'
+ arguments[i]
+ '" onclick="self.opener.clicker(\'' + id
+ '\',\'' + sugg0 + '\',\'' + sugg1
+ '\',\'' + sugg2 + '\',\'' + sugg3
+ '\',\'' + sugg4 + '\',\'' + sugg5
+ '\',\'' + type + '\',' + (i - 1)
+ ');">',
'<\/td>',
'<\/tr>');
}
}
message.push('<tr>',
'<td>',


[snip]

Once again: if you have problems, please post an example that
demonstrates this code in realistic use. There's no point in me
incrementally patching the code, and you waiting for me to do so, if you
can provide enough information so that I can debug before posting.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 23 '05 #13


Michael Winter wrote:
On 11/07/2005 17:17, Michael Winter wrote:

[snip]
elem = controls[(-1 != type.indexOf('HTML_COPY'))
? 'HTML_COPY'
: 'AOL_COPY'
];


I should have mentioned previously that if the type argument will always
be either 'HTML_COPY' or 'AOL_COPY', you can replace the above with:

elem = controls[type];

[snip]
function showSugg(id, sugg0, sugg1, sugg2,
sugg3, sugg4, sugg5, type)
{


[snip]
'<body>',
'<table>'];


'<body>',
'<table>'],
i;

/* The loop immediately below should perform the necessary
* entity replacements to make sure that any of the characters
* listed will not be inserted into the markup. Feel free to
* remove any that you know aren't a concern (if any).
*/
for(i = 2; i <= 6; ++i) {
arguments[i] = arguments[i]
? arguments[i].replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/&/g, '&amp;')
.replace(/'/g, '\\\'')
.replace(/"/g, '&quot;')
: '';
}
for(i = 2; i <= 6; ++i) {
if(arguments[i + 1]) {
message.push('<tr>',
'<td>' + arguments[i] + '<\/td>',
'<td>',
'<input type=radio name=radio value="'
+ arguments[i]
+ '" onclick="self.opener.clicker(\'' + id
+ '\',\'' + sugg0 + '\',\'' + sugg1
+ '\',\'' + sugg2 + '\',\'' + sugg3
+ '\',\'' + sugg4 + '\',\'' + sugg5
+ '\',\'' + type + '\',' + (i - 1)
+ ');">',
'<\/td>',
'<\/tr>');
}
}
message.push('<tr>',
'<td>',


[snip]

Once again: if you have problems, please post an example that
demonstrates this code in realistic use. There's no point in me
incrementally patching the code, and you waiting for me to do so, if you
can provide enough information so that I can debug before posting.

Mike


Michael, I appreciate all of your suggestions. I can't give you a URL
as this is intranet effort. After running the above code, the error
comes up as follows:
Error: missing ) after argument list
Source Code:
self.opener.clicker('a2','isn't','USN','INS','ins' ,'sin','ISBN','HTML_COPY',3);

I found another way to do this. Basically I substitute the string
"AQUOTE" for the apostrophe and let the user parse it accordingly. It
requires a trained eye to use the GUI, a small price for preserving
apostrophe. It'd be nice if JS has a function similar to escape to
handle quotes.

Many thanks once again.

Jul 23 '05 #14
ASM
so***********@yahoo.com wrote:

Thanks for getting back. The code to wrap the apostrophe in the double
quotes is giving syntax error on FireFox.

Error: syntax error
Source Code:
showSugg('a2',


Pfffooittt ! :-(

showSugg('a1','isnot','USN','INS','ins','sin','ISB N','HTML_COPY')

:-/

--
Stephane Moriaux et son [moins] vieux Mac
Jul 23 '05 #15
On 12/07/2005 17:42, so***********@yahoo.com wrote:

[snip]
It'd be nice if JS has a function similar to escape to handle quotes.


It usually isn't an issue. This is a problem only because you're writing
a string which must later be interpreted as a string literal.

Anyway, now that you've actually provided something close to test data,
I've had another crack at it. Hopefully, for the last time. :)

var display;

function clicker(id, sugg0, sugg1, sugg2, sugg3,
sugg4, sugg5, type, p)
{
var sugg = arguments[p + 1],
controls = document.forms[0].elements,
elem;

elem = controls[id];
elem.value = sugg;
if(elem.style) {
elem.style.backgroundColor = 'white';
elem.style.color = 'black';
}

elem = controls[type];
elem.value = elem.value.replace(new RegExp(sugg0, replaceOpts),
sugg);
return true;
}

function showSugg(id, sugg0, sugg1, sugg2,
sugg3, sugg4, sugg5, type)
{
var message = ['<html>',
'<head>',
'<title><\/title>',
'<style type="text/css">',
' body {font-family: Arial, sans-serif;}',
'<\/style>',
'<\/head>',
'<body>',
'<table>'],
d, i;

display = window.open('', '_blank');

for(i = 1; i <= 6; ++i) {
arguments[i] = arguments[i]
? arguments[i].replace(/&/g, '&amp;')
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
.replace(/'/g, '\\\'')
.replace(/"/g, '&quot;')
: '';
}

for(i = 2; i <= 6; ++i) {
if(arguments[i]) {
message.push('<tr>',
'<td>' + arguments[i] + '<\/td>',
'<td>',
'<input type=radio name=radio value="'
+ arguments[i]
+ '" onclick="self.opener.clicker(\'' + id
+ '\',\'' + sugg0 + '\',\'' + sugg1
+ '\',\'' + sugg2 + '\',\'' + sugg3
+ '\',\'' + sugg4 + '\',\'' + sugg5
+ '\',\'' + type + '\',' + (i - 1)
+ ');">',
'<\/td>',
'<\/tr>');
}
}
message.push('<tr>',
'<td>',
'<input type=button value=Replace '
+ 'onclick="self.opener.replaceOpts=\'i\';">',
'<\/td>',
'<td>',
'<input type=button value="Replace All" '
+ 'onclick="self.opener.replaceOpts=\'ig\';">',
'<\/td>',
'<td>',
'<input type=button value=Done '
+ 'onclick="self.opener.display.close();">',
'<\/td>',
'<\/tr>',
'<\/table>',
'<\/body>',
'<\/html>');

if(display && (d = display.document)
&& ('function' == typeof d.write)
&& ('function' == typeof d.close))
{
d.write(message.join('\n'));
d.close();
}
}

if('function' != typeof Array.prototype.push) {
Array.prototype.push = function(v) {
var i = this.length >>> 0,
j = 0,
n = arguments.length;

while(n > j) {this[i++] = arguments[j++];}
return (this.length = i);
};
}

This does work, assuming you have a form that contains controls
identified by the id and type arguments, and a global string variable
named replaceOpts.

I suspect that the clicker function could be simplified to take only
four arguments: id; type; what is, at the moment, sugg0; and one other,
rather than choosing between sugg1-5.

I also wonder why you have buttons that change state, and radio buttons
that do things. That really should be the other way around. For
instance, the radio buttons determine what value is used to alter the
form, and the Replace and Replace All buttons invoke that change.
On the subject of examples, I wasn't looking for the actual page. That
might complicate things, anyway. All that's necessary was a small
demonstration that illustrates how the function is used in practice, and
some sample data to go with it.

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jul 23 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terry Asher | last post by:
The following script does NOT escape the Apostrophe. Meaning when you mouseover the image the Alt tag says this: DMACC, It and then it stops. <SCRIPT Language="JavaScript"> var pos = "DMACC,...
5
by: KathyB | last post by:
Hi, not sure this is the right group, but hoping someone may have experienced this. I'm passing html text as a parameter to a javascript. When it has an apostrophe in it, of course it does parse...
2
by: Matthew Wieder | last post by:
In my previous post, I asked about a routine which prepares a string for an XPath query by taking care of escape characters. Unable to find a list, I'm now wondering assumign I enclose the...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
1
by: Harry Keck | last post by:
Is there a set of methods in the .Net framework that do the same thing as the jscript functions escape and unescape? I get very close with HttpUtility.UrlEncodeUnicode, but it puts "+" for...
7
by: Axel Dahmen | last post by:
Hi, within a DataGrid control I'm using a DataTable containing a string column to fill a Hyperlink's href attribute. Unfortunately HttpUtility.UrlEncode() doesn't escape the apostroph character,...
5
by: Thomas Beyerlein | last post by:
I have been looking for how to INSERT text with an apostrophe in it into SQL example: dim TEXT as string text = "TEST'S" INSERT INTO tbl_Test (String) VALUSES('" & TEXT & "')" This doesn't...
131
by: Lawrence D'Oliveiro | last post by:
The "escape" function in the "cgi" module escapes characters with special meanings in HTML. The ones that need escaping are '<', '&' and '"'. However, cgi.escape only escapes the quote character if...
2
by: Earl | last post by:
Anyone know why the RowFilter has to be double-escaped? Anticipating names with apostrophes, a single escape does not provide the proper name to filter on. For example, this would cause an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.