Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Crazy Quotes!

29 views
Skip to first unread message

justaguy

unread,
Jul 22, 2017, 11:57:09 PM7/22/17
to

var cond = 'yes',
lineX = '',
oid = 7;

if (cond == 'Yes') {lineX =
"<br><input type=button value=Update onclick=doSomething("+oid+")>" +
"&nbsp;&nbsp;&nbsp;&nbsp;" +
"<a href=mycomment.cfm?Number="+oid+" target=_new>Comment</a>";
}

var doSomething = function(oid) {
console.log(oid);
}


Of the above, the quotes for the line with "onclick=soSomething()..." drives me nuts. How to fix them?

Thanks.

Evertjan.

unread,
Jul 23, 2017, 4:51:12 AM7/23/17
to
justaguy <lichun...@gmail.com> wrote on 23 Jul 2017 in
comp.lang.javascript:

'use strict'; // add this for better debugging

> var cond = 'yes',

var cond = 'Yes'

// error[1]: case sensitive Yes

> lineX = '',
> oid = 7;
>
> if (cond == 'Yes') {lineX =
> "<br><input type=button value=Update onclick=doSomething("+oid+")>"

"<br><input type=button value=Update "
+ "onclick='doSomething("+oid+");'"

// error[2]: onclick needs [here single-]quoted content

> + "&nbsp;&nbsp;&nbsp;&nbsp;" +
> "<a href=mycomment.cfm?Number="+oid+" target=_new>Comment</a>";
> }
>
> var doSomething = function(oid) {
> console.log(oid);
>}

document.write(lineX);

// error[3]: you need the string to be inserted

>
> Of the above, the quotes for the line with "onclick=soSomething()..."
> drives me nuts. How to fix them?

You could easily have found this out by using the internal debugger
of your browser [I used Google-Chrome]:

<http://hannivoort.org/test/CrazyQuotes.jpg>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

justaguy

unread,
Jul 23, 2017, 9:17:50 AM7/23/17
to
On Sunday, July 23, 2017 at 4:51:12 AM UTC-4, Evertjan. wrote:
> wrote on 23 Jul 2017 in
> comp.lang.javascript:
>
> 'use strict'; // add this for better debugging
>
> > var cond = 'yes',
>
> var cond = 'Yes'
>
> // error[1]: case sensitive Yes
>
> > lineX = '',
> > oid = 7;
> >
> > if (cond == 'Yes') {lineX =
> > "<br><input type=button value=Update onclick=doSomething("+oid+")>"
>
> "<br><input type=button value=Update "
> + "onclick='doSomething("+oid+");'"
>
> // error[2]: onclick needs [here single-]quoted content
>
> > + "&nbsp;&nbsp;&nbsp;&nbsp;" +
> > "<a href=mycomment.cfm?Number="+oid+" target=_new>Comment</a>";
> > }
> >
> > var doSomething = function(oid) {
> > console.log(oid);
> >}
>
> document.write(lineX);
>
> // error[3]: you need the string to be inserted
>
> >
> > Of the above, the quotes for the line with "onclick=soSomething()..."
> > drives me nuts. How to fix them?
>
> You could easily have found this out by using the internal debugger
> of your browser [I used Google-Chrome]:
>

Thanks. But three things:
(1) The oid value needs to a string, ie, "NSS-892-A-233".
(2) Probably the ending ">" was missed in your
[ + "onclick='doSomething("+oid+");'" ] line
(3) For Chrome internal debugger, were you referring to dev tool's console tab?


Julio Di Egidio

unread,
Jul 23, 2017, 11:18:04 AM7/23/17
to
That code is just "ugly": the advice really is manipulate the DOM instead.

That said, it is not even just about escaping quotes within quotes: html
attributes should be html-encoded (no built-in function to do this, anyway
there are only 4 characters to escape: ", <, >, and &), plus url parameters
should be url-encoded (use encodeURIComponent there).

Anyway, assuming your attributes (your oid in particular) need no encoding,
here is how a single-quoted string looks like:

'<br><input type="button" value="Update" ' +
'onclick="doSomething(&quot;' + oid + '&quot;)">' +
'&nbsp;&nbsp;&nbsp;&nbsp;' +
'<a href="mycomment.cfm?Number=' + oid + '" target="_new">Comment</a>'

And here is the double-quoted one, for comparison:

"<br><input type=\"button\" value=\"Update\" " +
"onclick=\"doSomething(&quot;" + oid + "&quot;)\">" +
"&nbsp;&nbsp;&nbsp;&nbsp;" +
"<a href=\"mycomment.cfm?Number=" + oid + "\" target=\"_new\">Comment</a>"

Not tested, there may be typos: but you get the picture...

HTH,

Julio

Evertjan.

unread,
Jul 23, 2017, 11:24:47 AM7/23/17
to
justaguy <lichun...@gmail.com> wrote on 23 Jul 2017 in
You did specify a number value.
Is that reason to say "thanks, but" ???

>> > oid = 7;

var oid = '"NSS-892-A-233"';

> (2) Probably the ending ">" was missed in your
> [ + "onclick='doSomething("+oid+");'" ] line

You are unshure?

> (3) For Chrome internal debugger, were you referring to dev tool's
> console tab?

Why did you skip my link? The link gives the answer.

justaguy

unread,
Jul 23, 2017, 11:33:37 AM7/23/17
to
Yes, it works. So, lesson learned: the key here is to use the
&quot; in html format. Thought I tried to '" + oid + "' to no avail.

Thanks.


justaguy

unread,
Jul 23, 2017, 11:36:11 AM7/23/17
to
On Sunday, July 23, 2017 at 11:24:47 AM UTC-4, Evertjan. wrote:
> justaguy wrote on 23 Jul 2017 in
The most forgivable thing in the world is a typo type of omission !

> > (3) For Chrome internal debugger, were you referring to dev tool's
> > console tab?
>
> Why did you skip my link? The link gives the answer.

Sorry, I missed it. Thanks.

Julio Di Egidio

unread,
Jul 23, 2017, 12:01:19 PM7/23/17
to
Or you could have used single-quotes there, since single-quotes need not be
html-encoded:

'onclick="doSomething(\'' + oid + '\')">' +

or:

"onclick=\"doSomething('" + oid + "')\">" +

Still a terrible approach... ;)

Julio

justaguy

unread,
Jul 23, 2017, 11:14:55 PM7/23/17
to
Ok, thanks.
0 new messages