简体   繁体   English

Javascript:转义字符(\b、\f、\v)是否可以安全地用作用户输入数据和 localStorage 的连接符?

[英]Javascript: Are the escaped characters (\b, \f, \v) safe to use as concatenators for user entered data and localStorage?

According to this document these escape codes ( \b \f \v ) refer to backspace, form feed and vertical tabulator.根据本文档,这些转义码 ( \b \f \v ) 指的是退格、换页和垂直制表符。

I was thinking I could use one of these characters to concatenate some user entered data that I'm going to store in a localStorage key.我在想我可以使用这些字符之一来连接一些用户输入的数据,这些数据我将存储在 localStorage 键中。 As far as I'm aware there's no simple way a user would input one of these characters in a form field but I thought I would ask here to be sure.据我所知,用户没有简单的方法可以在表单字段中输入这些字符之一,但我想我会在这里问一下。

You can do that, but it is not how you should structure multiple data into one string.你可以这样做,但这不是你应该如何将多个数据构造成一个字符串。

Instead format your data as JSON with JSON.stringify .而是使用 JSON.stringify 将您的数据格式化为JSON.stringify And then to read those again, parse it to the original data with JSON.parse .然后再次阅读这些内容,使用JSON.parse将其解析为原始数据。

For instance, to store your input values:例如,要存储您的输入值:

let allMyInputValues = [
    document.querySelector("#input1").value,
    document.querySelector("#input2").value
    // ...etc
];
let json = JSON.stringify(allMyInputValues);
localStorage.setItem("myform", json);

And to read them again into the form:并将它们再次读入表格:

let json = localStorage.getItem("myform"); 
let allMyInputValues = JSON.parse(json);
document.querySelector("#input1").value = allMyInputValues[0];
document.querySelector("#input2").value = allMyInputValues[1];
// ...etc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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