简体   繁体   中英

Newline character breaking Javascript code

In my Ruby on Rails app this controller responds with Javscript render. For certain 'labels' I need to replace spaces with a newline character \\n . There are two ways to do this:

1) Replace the white spaces in db itself and just fetch and serve it in the front end.

This makes "North America" to " North\\nAmerica"

2) Replace it in the application code

label = label.gsub(" ","\n")

The second one breaks the javascript code. I looked at the request using firebug and the second one breaks the word, the newline character is actually interpreted as compared to the first implementation where it appears as plain text.

Context: The action renders a .js file as it's response to an ajax call. The js file calls Morris.js functions to implement analytics. See this question here too: svg-text-disappears-on-larger-label

It seems that you are having problem with javascript escaping in your code. If you are using remote request than you may use escape_javascript and in case you are using the script in html then you must escape it the way we normally do it using '\\'

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