I have an HTMLViewer that is loaded by creating the page with CSS, JavaScript, and the HTML combined and then displaying. The page includes an HTML form. The JavaScript includes a function called LoadData. When the page load is complete I call a method to call this LoadData function with the JSON data. The problem is that this fails if any of the data includes an apostrophe.
The JavaScript string I build to execute on the HTMLViewer includes apostrophes to enclose the JSON which uses double quotes for the items. (The data for the form is entered in another application. I need to be able to open and display in my Xojo app.)
I have tried escaping the apostrophe that is inside a data item but it hasn’t worked. Any ideas?
Here is what I have tried.
[code] Dim theJS As String
Dim theFormData As String
// escape the \r
characters or the data will not display
theFormData = ReplaceAll(Self.formData, "\r
", "\\r\
")
theFormData = ReplaceAll(Self.formData, “’”, “\’”)
theJS = “LoadData(’” + theFormData + “’);”
if (Len(theFormData) > 0) then
HTMLFormViewer.ExecuteJavaScript(theJS)
end if
// handle any errors
Exception err As HTMLViewerException
NotifyAlert(“HTML JavaScript”, “Error running the LoadData JavaScript.”, Str(err.ErrorNumber)+" “+err.Message)
#if TargetWin32 then
Exception err As OLEException
NotifyAlert(“HTML JavaScript”, “Error running the LoadData JavaScript.”, Str(err.ErrorNumber)+” "+err.Message)
#endif
[/code]
Always get an exception if there is an apostrophe in the data since it is taken as the end of the string.