简体   繁体   English

转义/删除<script> tags in JSON data parsed to HTML

[英]Escape/remove <script> tags in JSON data parsed to HTML

I have a data file that contains JSON objects to load data on my site. 我有一个数据文件,其中包含JSON对象以将数据加载到我的网站上。 I would like to know if there's a way to use Javascript to find and strip out any tags that may be within paragraph content. 我想知道是否有一种使用Javascript的方法来查找和删除段落内容中可能存在的任何标签。

Edit: i'm using handlebars locally. 编辑:我在本地使用车把。

Example: 例:

{
'foodType': 'breakfast bar',
'title': 'granola Bar',
'ingredients': 'oats, sesame seeds, sulphites, almonds <script>console.log("executed!")</script>, cashews, soy, stabilisers. May contain traces of peanuts, milk and other gluten cereals.'
},

I'm displaying the content like this: 我正在显示这样的内容:

for (var j=0; j<ingredientsList.length; j++){
var ingredients = $('<p/>');
ingredients.append(ingredientsList[j]);
$('.ingredients-sentences').append(ingredients);
}

How can I find the tag in my content and remove it before I build my ingredients paragraph? 在构建成分段落之前,如何在内容中找到标签并将其删除?

I've tried the following but feel like i'm going around in circles now: 我已经尝试了以下方法,但是感觉到我现在正在转圈:

if (ingredientsList.indexOf('<script>') > 0) {

var start = ingredientsList.indexOf('<script>'); 
var end = ingredientsList.indexOf('</s');

ingredientsList.slice(start, end);
}

Any advice? 有什么建议吗?

正如cschuff所说的...

ingredients.text(ingredientsList[j]);

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

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