PDA

View Full Version : unterminated string constant


jasongr
01-23-2005, 09:03 AM
Hello

I have a very frustrating problem which causes the following HTML code to trigger a 'unterminated string constant' error in the browser.
Here is my code.
When the link is clicked I would like to pass the following parameters to the selectIndividual function:
* id (integer)
* name (string)
* thumbnail (integer)
* gender (string)
I get the unterminated string constant error on the first ' right before the word John.
Can anyone see what the problem is and how it should be fixed?

<html>
<head>
<script type="text/javascript">
<!--
function selectIndividual(id, name, thumbnail, gender) {}

// -->
</script>
</head>
<body>
<a href="#" onclick="selectIndividual(594, 'John, Smith \"Charles\"', 0, 'M');">test</a>
</body>
</html>

regards

coothead
01-23-2005, 09:38 AM
Hi there jasongr,

if you wish "Charles" to be double quoted then try it like this...
<html>
<head>
<script type="text/javascript">
<!--
function selectIndividual(id, name, thumbnail, gender) {
alert(id);
alert(name);
alert(thumbnail);
alert(gender)
}
// -->
</script>

</head>
<body>

<div>
<a href="#" onclick='selectIndividual(594, "John, Smith \"Charles\"", 0, "M");'>test</a>
</div>
</body>
</html>
...also note that you should not use an interger as an id.

jasongr
01-23-2005, 10:02 AM
thanks for reply

note that I don't have control over the content of the string.
The string in my example was John, Smith "Charles" but it could have easily be John, Smith 'Charles'
Or a much more complex combination of the two like:
John's friend say: "Hello!"
this string contains both single and double quotes. The JavaScript and the HTML need to work with it with no problem

I am looking for a sound solution that will take care of all these cases

regards

ElectricSheep
01-23-2005, 11:48 AM
Okay, this is a possible solution that expands on Coothead's code.

It will depend on your being able to insert a character in place of the double and single quotes.
In my example I have chosen square brackets but most characters will do if you think a square bracket will be part of the string you need to retain.
The square brackets are converted to single or double quotes when processed by the function.

<html>
<head>
<script type="text/javascript">
<!--
function selectIndividual(id, name, thumbnail, gender) {

var name = name.replace(/\[\[/g,'"');
var name = name.replace(/\[/g,"'");

alert(id);
alert(name);
alert(thumbnail);
alert(gender);
}

//-->
</script>
</head>
<body>
<a href="#" onclick="selectIndividual(594, 'John, Smith [Charles[', 0, 'M');">test 1</a>
<a href="#" onclick="selectIndividual(594, 'John, Smith [[Charles[[', 0, 'M');">test 2</a>
</body>
</html>

Jon Hanlon
01-23-2005, 04:37 PM
Most modern browsers recognise character entities, so I usually replace quotes by the entity.
Replace ' by &amp;#39;
Replace " by &amp;#34; (or &amp;quot;)

The other way is to escape the string on the way in, and unescape it later.

http://msdn.microsoft.com/library/en-us/script56/html/js56jsmthescape.asp?frame=true

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsmthencodeuricomponent.asp