简体   繁体   中英

Escaping double quotes in event handler that does an alert, using Grails

Per this similar post , I tried the following but none-worked, with error noted to side:

onclick="alert('This is my  \"quoted text:\"');"   (no browser response)

onclick="alert('This is my  \\"quoted text:\\"');  (no browser response) 

onclick="alert('This is my  "quoted text:"');"  (comes out as: This is my  "quoted text:")

onclick="alert('This is my  \x22quoted text:\x22');"  (grails .gsp page preprocessor chokes on this)

Any ideas, other than putting the alert into a function? I imagine there is a way to do this in html/javascript?

NOTE: I updated the title to qualify the question for using Grails. All the solutions below may well work in general, but not in grails 1.3.7. What did work, per MaxArt's direction, is adding an additional backslash before each backslash, in version 4 above.

The last form is the correct one. I don't know how Grails handles that, but you may try to escape the backslashes.

You can always resolve to (and it's actually a better practice) attach an event listener to the element rather than using the traditional event declaration. This should be executed after the body has loaded:

document.getElementById("alerter").addEventListener("click", function() {alert('This is my "quoted text"');}, false);

Of course, use attachEvent for Internet Explorer < 9.

And this should be your element:

<span id="alerter">Quoted text</span>

Have you tried

&#8220; for a left
&#8221; for a right

Just another option.

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