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

string interpolation .... like perl

P: n/a
I am beginning JavaScript, expect to see me in here a bit more over
the next few months :) I am currently writing some HTML with
JavaScript. As you know HTML has many double quotes ( " ) when
declaring attibute values, I know in some cases quotes are optional,
but I like them.
Here is something I've been playing with , it works, but in my
opinion, it's ulgy...

<script>
var myArray = new Array("blue","green","red");
for(var i = 0; i < myArray.length; i++) {
document.write("<input type=\"submit\" value=\"" + myArray[i] +"\"
onClick=\"document.bgColor='" + myArray[i] +"'\">");
}
</script>

Is there any way to mix javascript variables in with HTML without
having to excape all of the qoutes?

a perlish solution would be the qq operatior. but I know of nothing in
javascript. Is there anything?

perlish example...

document.write(qq` an example of " any text" ... `);
TIA

w i l l
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

w i l l wrote:
I am beginning JavaScript, expect to see me in here a bit more over
the next few months :) I am currently writing some HTML with
JavaScript. As you know HTML has many double quotes ( " ) when
declaring attibute values, I know in some cases quotes are optional,
but I like them.
Here is something I've been playing with , it works, but in my
opinion, it's ulgy...

<script>
var myArray = new Array("blue","green","red");
for(var i = 0; i < myArray.length; i++) {
document.write("<input type=\"submit\" value=\"" + myArray[i] +"\"
onClick=\"document.bgColor='" + myArray[i] +"'\">");
}
</script>

Is there any way to mix javascript variables in with HTML without
having to excape all of the qoutes?
JavaScript considers " (double-quotes) and ' (single-quotes) as
equivalent (unlike other languages!) and allows you this kind of
construction:

var myString = 'This string says "Hello world"...';

However, there are situations where escaping is not avoidable. You will,
in your programmer's life, have to escape other characters too, like \\,
\t, \n, \r etc... After a while, you will stop noticing it and be able
to read escaped code as easily as unescaped ones. It's just a matter of
experience. It's the same in most advances languages anyway.

HTH,

Laurent
a perlish solution would be the qq operatior. but I know of nothing in
javascript. Is there anything?

perlish example...

document.write(qq` an example of " any text" ... `);
TIA

w i l l

--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #2

P: n/a
Make that

document.write('<input type="submit" value=' + myArray[i].quote() + '
onclick=' + ('document.bgColor=' + myArray[i].quote() + ';').quote() + '>');
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.