Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33614

Re: Crazy Quotes!

Newsgroups comp.lang.javascript
Date 2017-07-23 08:17 -0700
References <bc8875a5-1a83-4bc4-8653-66d85ab67ec8@googlegroups.com>
Message-ID <1a3f1888-e578-458c-9d0e-243efbb35ab9@googlegroups.com> (permalink)
Subject Re: Crazy Quotes!
From Julio Di Egidio <julio@diegidio.name>

Show all headers | View raw


On Sunday, July 23, 2017 at 5:57:09 AM UTC+2, justaguy wrote:
> 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?

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

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Crazy Quotes! justaguy <lichunshen84@gmail.com> - 2017-07-22 20:56 -0700
  Re: Crazy Quotes! "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-23 10:51 +0200
    Re: Crazy Quotes! justaguy <lichunshen84@gmail.com> - 2017-07-23 06:17 -0700
      Re: Crazy Quotes! "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-23 17:24 +0200
        Re: Crazy Quotes! justaguy <lichunshen84@gmail.com> - 2017-07-23 08:36 -0700
  Re: Crazy Quotes! Julio Di Egidio <julio@diegidio.name> - 2017-07-23 08:17 -0700
    Re: Crazy Quotes! justaguy <lichunshen84@gmail.com> - 2017-07-23 08:33 -0700
      Re: Crazy Quotes! Julio Di Egidio <julio@diegidio.name> - 2017-07-23 09:01 -0700
        Re: Crazy Quotes! justaguy <lichunshen84@gmail.com> - 2017-07-23 20:14 -0700

csiph-web