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

novice help! (single quote escaping)

P: n/a
hi, i can't seem to be able to escape my single quote properly... is it
even possible in javascript?

this is a portion of my code..

var DLEWIS="Pastor Lewis";

.... Sermon is a yser-defined class ..

var en_20031102=new Sermon("11/02",DLEWIS,"The Lord\'s Supper: The Art Of
Struggle",
"<a
href='http://www.biblegateway.com/cgi-bin/bible?language=english&passage=Lam
entations+3%3A19-27&version=NIV' target='_blank'>Lamentations<br />
3:19-27</a>",
"sermon-20031102-en.m3u","-");
.... and then in the printing function i write the following code:

document.write("<a href='"+sermonArray[i].notes+"'><img src='ppt.gif'
alt='");
document.write(sermonArray[i].title+" - "+sermonArray[i].speaker+" ||
"+sermonArray[i].date+"' /></a>");

for a sample of how it looks, you can go check out:
http://montreal-cac.org/worship/worship_en.html
is anyone able to give me advice? thanks.,.
Jul 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
sankofa wrote:
hi, i can't seem to be able to escape my single quote properly... is it
even possible in javascript? var en_20031102=new Sermon("11/02",DLEWIS,"The Lord\'s Supper: The Art Of
No need to escape the single quote here.
document.write("<a href='"+sermonArray[i].notes+"'><img src='ppt.gif'
alt='");
So you're using single quotes for the attribute quote, and you do have
single quotes in the string being written like in...
document.write(sermonArray[i].title+" - "+sermonArray[i].speaker+" ||
"+sermonArray[i].date+"' /></a>");


.... so the quick solution would be to use escaped double quotes for the
HTML attribute quote, starting at

document.write("<a href=\""+...

More generally, when writing HTML with javascript, you have to determine
whether you need javascript escapes or HTML escapes.

Basically, you can start from the output (HTML) to build your strings.
If you want something like

<tag attr="'foo'">

then you'll have to write something like

document.write("<tag attr=\"'foo'\">");

escaping the double quotes for the attributes for javascript. Inverting
the logic with single quotes work the same way.

Now if you need to have both single and double quotes in the quoted
attribute, your problem is clearly related with the HTML and the quoting
of the attribute, so you'll have to use HTML "escapes", i.e. characters
entities, for instance a desired output could be

<tag title="Hello 'World' and &quot;foo&quot;">

and the related javascript would be like

document.write("<tag title=\"Hello 'World' and &quot;foo&quot;\">");

Generally, problems are raised when concatenating strings many times in
javascript; the level of escape increases each time, with makes the code
difficult to maintain. The solution is to maximize the use of
references, concatenating only once.
HTH
Yep.
Jul 20 '05 #2

P: n/a
Yann-Erwan Perio hu kiteb:
document.write("<tag attr=\"'foo'\">");

escaping the double quotes for the attributes for javascript.
Inverting the logic with single quotes work the same way.


This doesn't strictly follow standards, but every browser I know of will
accept an attribute that has no quote proided that the attribute has no
spaces.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #3

P: n/a
In article <bo*************@ID-174912.news.uni-berlin.de>, "Fabian"
<la****@hotmail.com> writes:
Yann-Erwan Perio hu kiteb:
document.write("<tag attr=\"'foo'\">");

escaping the double quotes for the attributes for javascript.
Inverting the logic with single quotes work the same way.


This doesn't strictly follow standards, but every browser I know of will
accept an attribute that has no quote proided that the attribute has no
spaces.


Can you space that attr= part out and tell me which part of it was not quoted?
In fact, it was *double* quoted:

document.write("<tag attr= \ " ' foo ' \ " >");

Which writes this:
<tag attr=" ' foo ' " >

--
Randy
Jul 20 '05 #4

P: n/a
JRS: In article <3f**********************@news.free.fr>, seen in
news:comp.lang.javascript, Yann-Erwan Perio <y-*******@em-lyon.com>
posted at Sat, 8 Nov 2003 23:31:58 :-

<tag title="Hello 'World' and &quot;foo&quot;">

and the related javascript would be like

document.write("<tag title=\"Hello 'World' and &quot;foo&quot;\">");

Generally, problems are raised when concatenating strings many times in
javascript; the level of escape increases each time, with makes the code
difficult to maintain. The solution is to maximize the use of
references, concatenating only once.

That may cause confusion; you are using 'concatenation' to mean
'nesting'.

Concatenation : "pig" + 'tail'
Nesting : "n'oeuf'id" or "n\"oeuf\"id"

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.