LiveCode Server - Html, Quotes & JS?
Mark Wieder
ahsoftware at sonic.net
Mon Jun 19 15:36:54 EDT 2017
On 06/19/2017 11:22 AM, Rick Harrison via use-livecode wrote:
> I have a nice example button image swap routine
> written in JavaScript which Id like to use in a .lc script.
>
> To use HTML etc. within a .lc script I have to put
> the statement in double quotes such as:
>
> put <html>
>
> or
>
> put <A HREF=http://www.google.com' <http://www.google.com'/>>
>
> where I have to substitute single quotes for the normal double quotes to make it work.
>
> Thats fine until I have a statement that is already using both double and single quotes.
> The statement still needs to be converted into a .lc script format. The following statement
> will get parsed the wrong way if one wants to put it into double quotes.
>
> <A HREF="http://www.google.com" onmouseover="movepic('button','pic-off.gif')" onmouseout="movepic('button','pic-on.gif')"><IMG NAME="button" SRC="pic-off.gif" ALT="Image"></A>
>
> becomes:
>
> put "<A HREF="http://www.google.com" onmouseover="movepic('button','pic-off.gif')" onmouseout="movepic('button','pic-on.gif')"><IMG NAME="button" SRC="pic-off.gif" ALT="Image"></A>
>
> As you can see the above statement will not be parsed correctly.
>
> Suggestions?
>
> On a related note, if a user types in a word such as cant into a field, the apostrophe gets interpreted as
> a single quote and that messes up the parsing of various .lc script statements too.
>
> How do you usually solve these issues?
There's a long-standing bug report about wanting to use single- and
double- quotes interchangeably. But even that won't completely solve
your problem here.
I usually resort to a general-purpose function that Ken Ray posted some
years ago for quoting text when you can't do it explicitly:
function q pText
return quote & pText & quote
end q
So you'd end up with
put "<A HREF=" & q("http://www.google.com") && "onmouseover=" &
q("movepic('button','pic-off.gif')") && "onmouseout=" &
q("movepic('button','pic-on.gif')") && "><IMG NAME=" & q("button") &&
"SRC=" & q("pic-off.gif") && "ALT=" & q("Image") & "></A>
and that should work even if it's a bit ungainly.
To make it more readable, I usually break that up with constants.
Something like:
constant kURL="http://www.google.com"
constant kOnAction="movepic('button','pic-on.gif')"
constant kOffAction="movepic('button','pic-off.gif')"
local sImageContent
function href pURL, pContent
return "<A HREF=" & pURL & ">" & pContent & "</A>"
end href
put "<IMG NAME=" & q("button") && "SRC=" & q("pic-off.gif") && "ALT=" &
q("Image") & ">" into sImageContent
put href(q(kURL) && "onmouseover=" & q(kOnAction) && "onmouseout=" &
q(kOffAction), sImageContent)
--
Mark Wieder
ahsoftware at gmail.com
More information about the use-livecode
mailing list