visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: DHTML, JScript :: Knowledge Base : how to include single quotes in alert() or title= values ; no unterminated string ; apostrophes dans chaînes


By: VGR France  Date: 31/08/2006 16:23:30  English French  Points: 0 Status: Answered
Quality : Excellent
Ceci est surtout utile aux non-anglophones.

One of the nastiest problem I encountered for more than ten years in HTML is the difficulty to include single quotes (') in the strings used in Javascript, especially in alert() boxes, title= values for images and links, or for the status line.

For instance when you are doing in PHP :

$delicon="<img src='$courant/images/icon_del.gif' alt='$loctxtdel' title='$loctxtdel' style='vertical-align:middle;' OnClick='alert(\"Impossible d\'effacer l\'éditorial!\");' class='clickableimg'>";



You end up this this seemingly valid HTML :

<img src='images/icon_del.gif' alt='EFFACER' title='EFFACER' style='vertical-align:middle;' OnClick='alert("Impossible d\'effacer l\'éditorial!");' class='clickableimg'>



This is becoming to be VERY painful to only receive this kind of Javascript console result :

Error: unterminated string literal Source File: http://localhost/varapp/admin_breves.php Line: 1, Column: 6 Source Code: alert("Impossible d



And of course the link doesn't work when clicked on.

You can try to escape characters, backslash here and there but if you don't succeed then read on ;-)

There is always the well-known and widely-used "solution" of NOT having any quote in your TITLE='xxx', alert('xxx') calls or status line values, but "the" solution is "simply" to use the HTML entity & apos ; (&apos;) in stead :D

The only drawback of this solution (the site works fine, the quotes do appear in the alert() boxes, etc) is this strange (and stupid) Tidy warning :

line 71 column 201 - Warning: named entity &apos; only defined in XML/XHTML



You won't have this if you use an XHTML DTD, of course, but I'm using preferably HTML 4.01 so I must bear with the warning.

Happy coding.

Do register to be able to answer

EContact
browser fav
page generated in 368.613960 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page