Author |
Topic  |
TerryG
Junior Member
 
United Kingdom
179 Posts |
Posted - 13 February 2002 : 08:52:01
|
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 |
|
ruirib
Snitz Forums Admin
    
Portugal
26364 Posts |
Posted - 13 February 2002 : 09:33:46
|
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 |
 |
|
cevans
Junior Member
 
Canada
101 Posts |
Posted - 13 February 2002 : 09:38:23
|
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 |
 |
|
TerryG
Junior Member
 
United Kingdom
179 Posts |
Posted - 13 February 2002 : 09:40:25
|
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
|
 |
|
ruirib
Snitz Forums Admin
    
Portugal
26364 Posts |
Posted - 13 February 2002 : 09:41:23
|
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 |
 |
|
TerryG
Junior Member
 
United Kingdom
179 Posts |
Posted - 13 February 2002 : 10:18:21
|
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 |
 |
|
Kat
Advanced Member
    
United Kingdom
3065 Posts |
Posted - 13 February 2002 : 10:39:52
|
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 |
 |
|
Kat
Advanced Member
    
United Kingdom
3065 Posts |
|
ruirib
Snitz Forums Admin
    
Portugal
26364 Posts |
Posted - 13 February 2002 : 10:52:31
|
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...
|
 |
|
ruirib
Snitz Forums Admin
    
Portugal
26364 Posts |
Posted - 13 February 2002 : 10:57:51
|
I've just tried adding the & #8217 to a string used for an ALT of an image, and it works...
|
 |
|
Kat
Advanced Member
    
United Kingdom
3065 Posts |
Posted - 13 February 2002 : 11:04:42
|
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
|
 |
|
ruirib
Snitz Forums Admin
    
Portugal
26364 Posts |
Posted - 13 February 2002 : 11:28:19
|
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...
|
 |
|
ruirib
Snitz Forums Admin
    
Portugal
26364 Posts |
Posted - 13 February 2002 : 12:13:34
|
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...
|
 |
|
TerryG
Junior Member
 
United Kingdom
179 Posts |
Posted - 13 February 2002 : 12:41:49
|
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
|
 |
|
TerryG
Junior Member
 
United Kingdom
179 Posts |
Posted - 13 February 2002 : 12:52:53
|
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).
|
 |
|
Topic  |
|