how to escape apostrophe

S

soup_or_power

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,location=no,status=no,directories=no,toolbar=no,scrollbars=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();

}
 
E

Evertjan.

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
 
A

ASM

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 ?
 
S

soup_or_power

ASM said:
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.
 
L

Lee

(e-mail address removed) 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.
 
M

Michael Winter

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
 
A

ASM

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
 
M

Michael Winter

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
 
S

soup_or_power

Michael said:
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','ISBN','HTML_COPY')
 
A

ASM

Also FireFox gave this error:

Error: missing ) after argument list
Source Code:
showSugg('a1','isn't','USN','INS','ins','sin','ISBN','HTML_COPY')
and :
showSugg(a1',"isn't",'USN','INS','ins','sin','ISBN','HTML_COPY')
what that gives ?
 
S

soup_or_power

ASM said:
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',
 
M

Michael Winter

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 = arguments
? arguments.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 + '<\/td>',
'<td>',
'<input type=radio name=radio value="'
+ arguments
+ '" 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
 
S

soup_or_power

Michael said:
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 = arguments
? arguments.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 + '<\/td>',
'<td>',
'<input type=radio name=radio value="'
+ arguments
+ '" 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.
 
A

ASM

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','ISBN','HTML_COPY')

:-/
 
M

Michael Winter

On 12/07/2005 17:42, (e-mail address removed) 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 = arguments
? arguments.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
.replace(/'/g, '\\\'')
.replace(/"/g, '&quot;')
: '';
}

for(i = 2; i <= 6; ++i) {
if(arguments) {
message.push('<tr>',
'<td>' + arguments + '<\/td>',
'<td>',
'<input type=radio name=radio value="'
+ arguments
+ '" 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top