javascript - 停止通过 x-editable 输入 <script>

标签 javascript jquery 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> ,然后我们看到 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'
}
});

JavaScript String replace() Method

关于javascript - 停止通过 x-editable 输入 &lt;script&gt;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579032/

相关文章:

javascript - 如何使用javascript从数组中填充无序列表?

javascript - 如何使用javascript检查它是否是一个blob url

c# - 在 MVC ASP.net 中使用 X-editable 时不要重复自己的最佳方法

javascript - 是否可以为 wookmark 中的特定元素设置宽度?

javascript - 无法在 Express 生成器上执行 npm start

javascript - Jquery 日期规则在 Firefox 和 IE 中不起作用

jquery - Bootstrap Datetimepicker 和 X-editable - 解析错误

codeigniter - x 可编辑和代码点火器 : display error message if data not updated into db

javascript - 如何在 JavaScript 中获得两个月(YYYYMM)之间的差异?

javascript - jQuery - 减少滚动函数中的代码的技术