Jquery-te如何阻止javascript执行?

时间:2014-09-08 15:34:38

标签: javascript jquery

我支持的应用程序使用jquery-te:http://jqueryte.com/demos。当我插入html编辑器的javascript代码(如alert(“Hello!”))时,它会立即执行。如何在编辑html-text期间禁用它的执行?

1 个答案:

答案 0 :(得分:1)

我通过编辑jquery-te-1.4.0.js文件中的源代码解决了这个问题。 起初,我添加了3个新变量:

    var scripts = [];
    var htmlWithoutScripts = "";
    var scriptPattern = /<script\b[^>]*>(.*?)<\/script>/g;

第二步是在第263行编辑js-code:

editor.attr("contenteditable","true").html(thisElementVal);

已更改为

scripts = thisElementVal.match(scriptPattern);
htmlWithoutScripts = thisElementVal.replace(scriptPattern , "");
editor.attr("contenteditable","true").html(htmlWithoutScripts);
editor.attr("contenteditable","true").val(thisElementVal);

第三个是改变函数postToSource()中的逻辑(第988行)。 添加了:

if(scripts !== null && scripts.length > 0)
{
      scripts.forEach(function(element){
      sourceStrings = sourceStrings.concat(element);
      });
}

之前:

thisElement.val(extractToText(sourceStrings));

最后一个是改变函数postToEditor()中的逻辑(第997行)。

移除:

editor.html(extractToText(thisElement.val()));

添加了:

var htmlContent = thisElement.val();
scripts = htmlContent.match(scriptPattern );
htmlWithoutScripts = htmlContent.replace(scriptPattern , "");
editor.html(extractToText(htmlWithoutScripts));
editor.val(extractToText(htmlContent));

如果你有一些评论,请写下来表示赞赏。

相关问题