Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot replace quotation mark "Unterminated string constant" error

Status
Not open for further replies.

JGKWORK

IS-IT--Management
Apr 1, 2003
342
GB
Hi, I'm having difficulties getting apostrophes to work in Javascript - with my code below (the Surname).I recieve the "Unterminated string constant" error with and without the replace function present. Obviously, I am assigning the variables from ASP - Can someone help? Thanks..

<script language="JavaScript">
{
VarSurname = <%=srchSurname%>
formattedSurname = VarSurname.replace("'","\'")

VarDocFrm.Job_Number.value = '<%=srchJobNumber%>'
VarDocFrm.Customer_Surname.value = formattedSurname
 
Maybe try this:

VarSurname = "<%=srchSurname%>"
formattedSurname = VarSurname.replace("'","\'")

*cLFlaVA
----------------------------
When will I be able to see what other members can see about myself? It's been like, a freakin' month already!
 
Additionally, you're gonna need to add an extra backslash to your replace function if you want this:

'blah'blah'blah'

to turn to this:

\'blah\'blah\'blah\'

"'" is the same as "\'" to javascript.

You'll need to write it out like so:

.replace("'", "\\'")

-kaht

banghead.gif
 
Many thanks, will try these out and let you know how I go..
 

Excellent many thanks cLFlaVA (and kaht) works great! Does this mean that apostrophes and inverted commas are both used to delimit text in Javascript?

Does this also mean that the apostrophes in the code below are being changed to a \ which is throwing everything out?

VarSurname = '<%=srchSurname%>'

Thanks again.
 
You can use single-quotes or double-quotes to surround a string literal.

Both of these will work:

[tt]var sur_name = '<%=srchSurname%>'
var sur_name = "<%=srchSurname%>"[/tt]

*cLFlaVA
----------------------------
Breaking the habit...
 
this is a better way as if ur ASP value contains ' it will bomb in javascript.

formattedSurname = '<%=replace(srchSurname,"'","\'")%>'


Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top