Languages :: DHTML, JScript :: Knowledge Base : how to include single quotes in alert() or title= values ; no unterminated string ; apostrophes dans chaînes |
|||
| By: VGR |
Date: 31/08/2006 16:23:30 |
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 ; (') 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 ' 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 |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








