Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 Apostrophes in strings
 New Topic  Topic Locked
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

TerryG
Junior Member

United Kingdom
179 Posts

Posted - 13 February 2002 :  08:52:01  Show Profile
I have some ASP which writes the following to the browser as part of my page:
<Img Src='image.gif' Border='0' ALT="Next Best Contact is J O'Neill"></A>
The name J O'Neill is a field from a database and all is OK even though its got an apostrophe in it.

However I also have code elsewhere which writes the following to the browser:
<A HREF='#' OnClick='FindNB("J O'Neill")'>Something here</A>

Now the apostrophe in "J O'Neill" causes an unterminated string error even before the link is used to submit the "J O'Neill" to the findNB() function, why?

Is my only option to use Replace() to change the apostropohe to something else like ~ and then to change it back in the javascript function?





Edited by - TerryG on 13 February 2002 08:55:39

Kat
Advanced Member

United Kingdom
3065 Posts

Posted - 13 February 2002 :  09:05:35  Show Profile  Visit Kat's Homepage
you could try using escape and unescape in javascript to encode and decode it.

KatsKorner

For Installation help: http://www.aslickpage.com/snitz_help.html

Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 13 February 2002 :  09:33:46  Show Profile  Send ruirib a Yahoo! Message
You can use something like:


<Img Src='image.gif' Border='0' ALT="Next Best Contact is J O& #8216;Neill"></a>

& #8216, without the space between the '&' and the '#', is the left quote char. I had to leave the space between the chars because otherwise the left quote char would show up here, instead of the code representing it.
So remove the space and you have what you were looking for.




Edited by - ruirib on 13 February 2002 09:37:20
Go to Top of Page

cevans
Junior Member

Canada
101 Posts

Posted - 13 February 2002 :  09:38:23  Show Profile  Send cevans an ICQ Message
You don't need to change the apostrophe to something else. You can replace it with a backslash and an apostrophe (eg. "\'") to escape it and treat it as a literal character in the javascript.

Clark
Go to Top of Page

TerryG
Junior Member

United Kingdom
179 Posts

Posted - 13 February 2002 :  09:40:25  Show Profile
I got it to work by using replace() to change it to another character which is then changed back in the javascript function however
quote:

you could try using escape and unescape in javascript to encode and decode it.
KatsKorner

For Installation help: http://www.aslickpage.com/snitz_help.html



This sounds much better so sorry to be so dim but could you give me an example ( or point me in the direction of one) Thanks

Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 13 February 2002 :  09:41:23  Show Profile  Send ruirib a Yahoo! Message
Hey, hey, you're kidding me . You changed your original message after I published my own.

It is easier to do it now. Just use Server.HTMLEncode() on the recordset field containing your value:


<Img Src='image.gif' Border='0' ALT="Next Best Contact is <%=Server.HTMLEncode(rs("whateverYourFieldIs"))%> "




Edited by - ruirib on 13 February 2002 09:42:16
Go to Top of Page

TerryG
Junior Member

United Kingdom
179 Posts

Posted - 13 February 2002 :  10:18:21  Show Profile
quote:

Hey, hey, you're kidding me . You changed your original message after I published my own.


Sorry about that, didn't expect such a fast response, should have known better at Snitz
quote:

It is easier to do it now. Just use Server.HTMLEncode() on the recordset field containing your value:


<Img Src='image.gif' Border='0' ALT="Next Best Contact is <%=Server.HTMLEncode(rs("whateverYourFieldIs"))%> "



Tried that, its not working for me though (still getting unterminated string error). I read up on Server.HTMLEncode and got the impression that its for getting strings containing characters like % & etc to display correctly when rendered on the page by the browser. The code I am trying to create on the clientside is not visible on the page so it wont help will it? or am I getting this all wrong?



Edited by - TerryG on 13 February 2002 10:20:31
Go to Top of Page

Kat
Advanced Member

United Kingdom
3065 Posts

Posted - 13 February 2002 :  10:39:52  Show Profile  Visit Kat's Homepage
Server.HTMLEncode is indeed server-side and will not work in JavaScript.

Did you try the suggestion of using a backslash before your single apostrophe? Or use the escape and unescape?

KatsKorner

For Installation help: http://www.aslickpage.com/snitz_help.html


sorry I only just saw your request for an example.. gimme a minute...


Edited by - kat on 13 February 2002 10:40:45
Go to Top of Page

Kat
Advanced Member

United Kingdom
3065 Posts

Posted - 13 February 2002 :  10:42:43  Show Profile  Visit Kat's Homepage
Try taking a look at this article: http://developer.netscape.com/docs/manuals/communicator/jsref/glob7.htm

KatsKorner

For Installation help: http://www.aslickpage.com/snitz_help.html

Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 13 February 2002 :  10:52:31  Show Profile  Send ruirib a Yahoo! Message
quote:

quote:

Hey, hey, you're kidding me . You changed your original message after I published my own.


Sorry about that, didn't expect such a fast response, should have known better at Snitz


No problem about that really. Just kidding . But your change sort of invalidated my first answer...
quote:

quote:

It is easier to do it now. Just use Server.HTMLEncode() on the recordset field containing your value:


<Img Src='image.gif' Border='0' ALT="Next Best Contact is <%=Server.HTMLEncode(rs("whateverYourFieldIs"))%> "



Tried that, its not working for me though (still getting unterminated string error). I read up on Server.HTMLEncode and got the impression that its for getting strings containing characters like % & etc to display correctly when rendered on the page by the browser. The code I am trying to create on the clientside is not visible on the page so it wont help will it? or am I getting this all wrong?


Not really. Just the other day I participated in a discussion in which Server.HTMLEncode was used to overcome a similar situation, just with a double quote, instead of a single quote.

Can you post the syntax you were using for Server.HTMLEncode. I think it should work. It did with the double quote, so I can't see any reason for not working with a single quote.

Visible or not visible is not the issue here, I think. It still is HTML. And it becomes visible when you move the mouse over the image..

Kat,
Why would you need to use Javascript for this? I think it can be sorted out using HTML generated by the server. I may be wrong, but I'm not convinced of it yet...

Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 13 February 2002 :  10:57:51  Show Profile  Send ruirib a Yahoo! Message
I've just tried adding the & #8217 to a string used for an ALT of an image, and it works...

Go to Top of Page

Kat
Advanced Member

United Kingdom
3065 Posts

Posted - 13 February 2002 :  11:04:42  Show Profile  Visit Kat's Homepage
You could just use Server.HTMLEncode but the chap is passing the value into a Javascript function so using escape is just as easy.

But if your solution works then that is probably easiest for now

KatsKorner

For Installation help: http://www.aslickpage.com/snitz_help.html

Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 13 February 2002 :  11:28:19  Show Profile  Send ruirib a Yahoo! Message
Well, I thought he was trying to use Javascript as a way to overcome this situation...
If he could solve it without Javascript it would be easier, I think...
That's why I've been pursuing this course...

Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 13 February 2002 :  12:13:34  Show Profile  Send ruirib a Yahoo! Message
I've just completed a final test. I'm using a similar situation as described by TerryG, reading a field, containing the "'" char, from a database to use as a "title" tag in a link field, which from a pratical point is the same as Terry's original situation. I used Server.HTMLEncode to convert the field read from the DB and it worked.

Here is my code:

Response.Write rsNews("Abstract") & "<span class=""newslink""> <a href=""newsdetail.asp?ID=" & rsNews("IDNews") & """" & _
"title=""" & Server.HTMLEncode(rsNews("Title")) & """ >" & "[read more]</a> </span>" & "</td></tr>" & vbCrLf


The result of this is that the title of the news is shown when you hover with the mouse over the "read more" link. The title shown has a "'" in the middle.
It worked perfectly.

There is no difference between TerryG situation and this one, so this can be done...

Go to Top of Page

TerryG
Junior Member

United Kingdom
179 Posts

Posted - 13 February 2002 :  12:41:49  Show Profile
First of all a BIG thankyou to everyone who helped with this.

We have gone a little off beam with this, my original post gave the example with the Alt tag as something which worked as written, the one that didnt work is the onclick='FindNB("DBFieldValue")'>, maybe this is why you are expecting some stuff to work and when I try it it doesn't.

Anyways it turns out that, as suggested on at least 2 occasions, I just needed to use the escape character, so my ASP now looks like this:
Response.Write"<A HREF='#' OnClick=""FindNB('" & replace((rsQuickResults.Fields.Item("IfUnnavail").Value),"'","\'",1,-1,1) & "')"">
Which gives me <A HREF='#' OnClick="FindNB('J O\'Neill')"> when its written to the browser, which does not cause an unterminated string error and works fine when its passed to the javascript function.

Now in the process of getting this far I noticed that this works-
<SCRIPT>
function findNB(test) {
alert(test);
}
</SCRIPT>
</HEAD>
<BODY bgcolor="#FFFFFF" text="#000000">
<a href='#' Onclick="findNB('J O\'NEILL')">Something</A>
</BODY>

But if I switch the apostrophes around like so
<a href='#' Onclick='findNB("J O\'NEILL")'>Something</A>
I am back to the unterminated string error - why? surely escape characters escape whatever follows them, regardless of whether the string is contained within " " or ' ', but in this instance anyway apparently not.
If anyone can explain I will be grateful.
Thanks again


Go to Top of Page

TerryG
Junior Member

United Kingdom
179 Posts

Posted - 13 February 2002 :  12:52:53  Show Profile
Just realised that in the time its taken to write the above ruirib
has posted again, and done a bundle of work on this, many thanks ruirib. Kat is right, I was not using the javascript to overcome the problem, it does something else. Ruirib, Does it still work if you response.write the code into a onClick=FunctionName() like in my post above ( I quite understand if you have had enough of me by now).


Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Topic Locked
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.99 seconds. Powered By: Snitz Forums 2000 Version 3.4.07