停止通过 x-editable 输入 <script>
Stop <script> being entered through x-editable
我们使用 x-editable 来快速编辑行。我们注意到一个小错误。当 <script>
标签被输入到可编辑输入中时,它将被执行。
<div class="test"></div>
$('.edit').editable({
params: function(params) {
var data = {};
data['id'] = params.pk;
data[params.name] = params.value;
return data;
},
success: function(response, newValue){
$(".test").html(newValue);
//if newValue will be <script>alert('hello')</scipt>
// then we see alert message with 'hello'
}
});
例如,如果 newValue
的字符串值为 <script>alert('hello')</script>
,那么我们会看到带有 'hello' 的 alert
消息。
我们怎样才能阻止这种行为?
将输入中的字符串 "script" 替换为 "code"... 这样它将输出为 "text"。可能是这样的……
$('.edit').editable({
params: function(params) {
var data = {};
data['id'] = params.pk;
data[params.name] = params.value;
return data;
},
success: function(response, newValue){
//gi: Perform a global, case-insensitive replacement:
newValue = newValue.replace(/script/gi, "code");
$(".test").html(newValue);
//if newValue will be <script>alert('hello')</scipt>
// then we see alert message with 'hello'
}
});
我们使用 x-editable 来快速编辑行。我们注意到一个小错误。当 <script>
标签被输入到可编辑输入中时,它将被执行。
<div class="test"></div>
$('.edit').editable({
params: function(params) {
var data = {};
data['id'] = params.pk;
data[params.name] = params.value;
return data;
},
success: function(response, newValue){
$(".test").html(newValue);
//if newValue will be <script>alert('hello')</scipt>
// then we see alert message with 'hello'
}
});
例如,如果 newValue
的字符串值为 <script>alert('hello')</script>
,那么我们会看到带有 'hello' 的 alert
消息。
我们怎样才能阻止这种行为?
将输入中的字符串 "script" 替换为 "code"... 这样它将输出为 "text"。可能是这样的……
$('.edit').editable({
params: function(params) {
var data = {};
data['id'] = params.pk;
data[params.name] = params.value;
return data;
},
success: function(response, newValue){
//gi: Perform a global, case-insensitive replacement:
newValue = newValue.replace(/script/gi, "code");
$(".test").html(newValue);
//if newValue will be <script>alert('hello')</scipt>
// then we see alert message with 'hello'
}
});