I have a web app in Node.js/MySQL where users can upload their stories. They write in an HTML textarea tag. Now I'm trying to get the uploaded from the database using ejs into a script tag so I can do further 'processes'
我在Node.js / MySQL中有一个Web应用程序,用户可以上传他们的故事。他们用HTML textarea标签写。现在,我正在尝试使用ejs将数据库上传到脚本标记,以便我可以进一步“进程”
<script>
var text = "<%=story.Content%>",
result = anchorme.js(text);
document.getElementById('story-content').innerHTML = twemoji.parse(result);
</script>
Problem is if the user hit enter to start on a new line while writing. It'll give me an error here in the text variable and nothing will be printed so how do I fix this?
问题是用户在写入时是否按Enter键开始新行。它会在文本变量中给出一个错误,并且不会打印任何内容,所以我该如何解决这个问题呢?
1 个解决方案
#1
0
If you view source on the page so that you can see how the browser receives it, you'll see something like this - note the line feeds:
如果您在页面上查看源代码以便您可以看到浏览器如何接收它,您会看到类似的内容 - 请注意换行符:
var text = "I am a story over multiple lines
and that's what javascript gets confused about
because it can't figure out where the closing quotes are.
Let's not even go into what could happen when someone uses quotes!"
So you really just need a way to pass the story content to javascript without it breaking the page. Instead of just pushing out the string like this...
所以你真的只需要一种方法将故事内容传递给javascript,而不会破坏页面。而不是像这样推出字符串......
var text = "<%=story.Content%>"
...you can pass stuff to javascript in JSON format as that allows and escapes newlines into \r\n and quotes into \" and so-on. Note the use of <%-
rather than <%=
here because you don't want it to pass escaped HTML:
...你可以将东西传递给JSON格式的javascript,因为它允许并将换行符转换为\ r \ n并将引号转换为\“等等。注意使用<% - 而不是<%= here因为你不喜欢我希望它传递转义的HTML:
var text = <%-JSON.stringify({ content: story.Content })%>.content;
That passes an object with a nicely escaped string in it to your inline script for it to carry on processing.
这将一个带有一个很好的转义字符串的对象传递给你的内联脚本,以便进行处理。
#1
0
If you view source on the page so that you can see how the browser receives it, you'll see something like this - note the line feeds:
如果您在页面上查看源代码以便您可以看到浏览器如何接收它,您会看到类似的内容 - 请注意换行符:
var text = "I am a story over multiple lines
and that's what javascript gets confused about
because it can't figure out where the closing quotes are.
Let's not even go into what could happen when someone uses quotes!"
So you really just need a way to pass the story content to javascript without it breaking the page. Instead of just pushing out the string like this...
所以你真的只需要一种方法将故事内容传递给javascript,而不会破坏页面。而不是像这样推出字符串......
var text = "<%=story.Content%>"
...you can pass stuff to javascript in JSON format as that allows and escapes newlines into \r\n and quotes into \" and so-on. Note the use of <%-
rather than <%=
here because you don't want it to pass escaped HTML:
...你可以将东西传递给JSON格式的javascript,因为它允许并将换行符转换为\ r \ n并将引号转换为\“等等。注意使用<% - 而不是<%= here因为你不喜欢我希望它传递转义的HTML:
var text = <%-JSON.stringify({ content: story.Content })%>.content;
That passes an object with a nicely escaped string in it to your inline script for it to carry on processing.
这将一个带有一个很好的转义字符串的对象传递给你的内联脚本,以便进行处理。