简体   繁体   中英

Grails String with html code breaking javascript(cordova)

In my grails application I have this String

String description = "<p>Some long description here that shows me the message in javascript</p><span data-icon=\"pushpin\"><p>But here doesnt give me any message in javascript"</p>

But when I try to recover the String to do an alert or something in javascrip, in my cordova project:

var button = '<button type="button" onclick="getInfo("' + x.description + '")">More infomation</button>';`

It only gives me the me the first <p></p> and writes inside the button, I know its from the String "pushpin" but there's a way to fix the "" or '' ?

I'd like to not use jQuery.

I suspect your issues come from the fact the double/single quotes aren't escaped when the value is rendered. Try using the encodeAsJavascript() function. You may also want to read the documentation about this.

x.description.encodeAsJavaScript()

Or in the controller (since you aren't using GSP)

def String description = "<p>Some long description here that shows me the message in javascript</p><span data-icon=\"pushpin\"><p>But here doesnt give me any message in javascript</p>".encodeAsJavaScript()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM