I want to insert the result of a query into a javascript array.
I have a problem: into the result could be HTML Tags, so i try to Encode the string.
But it doesn’t work.
Here’s my code:
Response.Write "<script type='text/javascript'>"& vbCrLf
Response.Write "var myArr = new Array();"
z=0
Do While not rsScadenze.EOF
If (rsScadenze("scadenza")<> "") Then
encodeString =Server.HtmlEncode(rsScadenze("testo"))
Response.Write "myArr["& z &"]=('"& encodeString &"');"& vbCrLf
z=z+1
End If
rsScadenze.MoveNext
Loop
Response.Write "</script>"
rsScadenze.close
How can i do?
Thanks for helping me
Thank you for the accurate answer.
I made the changes but it doesn’t still work, i’m going crazy. The HTML don’t generate nothing, only a blank sheet with no errors.
So i looked the source code of the page, that my code generates:
<script>
var myArr = new Array();
myArr[0]=('<div style="text-align: justify"><span style=& quot;font-size: 11pt">Per i contribuenti che effettuano operazioni con operatori economici aventi sede, residenza o domicilio negli Stati o territori dei Paesi c.d. &ldquo;<i>black-list</i>&rdquo; scade oggi il termine di presentazione degli elenchi riepilogativi delle operazioni effettate nel mese precedente, per i contribuenti tenuti a questo adempimento con cadenza mensile.</span></div>');
document.write (myArr[0]);
... and so on, until the last element of my array
I tried to change the content of encodeString writing manually a string (encodeString =”…”), and in this case the code works well.
Why?
Thanks a lot
Try a few things…
first, break the string that starts with [script] into something like this
response.write "<scr" & "ipt" & ...asp does not like to see the word “< script >” being response.written!
do the same to the closer
response.write "</sc" & "ript>"using the script and /script like that confuses the asp.dll.
this is a generic tip and so always do it the way I suggest it.
as to your code, you can try this;
change
if rsScadenze("scadenza")<> "") Thento
if "" & rsScadenze("scadenza")<> "" ) Thenthis way, you can catch the nulls too
change
encodeString =Server.HtmlEncode(rsScadenze("testo"))to
encodeString = Server.HtmlEncode(rsScadenze("testo"))encodeString = replace(encodeString,vbcrlf," ",1,-1,1)encodeString = replace(encodeString,vbcr," ",1,-1,1)encodeString = replace(encodeString,vblf," ",1,-1,1)`’the above 3 lines would take care of any issue with line breaks
encodeString = replace(encodeString,"""",""",1,-1,1)'the above line would take care of any occurrences of "the rest of your code looks fine…
try these recommendations and let us know, but this time include the html ( or the errors ) generated pls.
HTH