字符串js编码转换成实体html编码的方法(防范XSS攻击)
js代码在html页面中转换成实体html编码的方法一:
<!DOCTYPE html>
<html>
<head>
<title>js代码转换成实体html</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="description" content="">
<meta name="keywords" content="">
<script src="http://www.xueersi.com/lib/jQuery/1.11.1/jquery.min.js?1458897912"></script>
</head>
<body>
<textarea id="strs">
<div style="color:red">js代码转换成实体html--红</div>
<script>
function test(){
alert('测试红')
}
test();
</script>
<div style="color:green">js代码转换成实体html--绿</div>
<script type="text/javascript">
alert('测试绿')
</script>
</textarea>
<div id="scriptCode"></div>
<div>
直接在html页面写法是这样写即可:
方法一:
<script>
alert("测试");
</script>
</div>
<p>
方法二:
<script >
function test1(x,y){
return x+y ;
}
alert(test1(5,2))
</script>
</p>
</body>
</html>
<script type="text/javascript">
function htmlspecialchars(str){
/*//转换所有的html标签
str = str.replace(/</g, '<');
str = str.replace(/>/gi,'>');*/
//只转换js的script标签
str = str.replace(/<script/g, '<script');
str = str.replace(/<\/script>/gi,'</script>');
return str;
}
var str = $.trim($('#strs').val());
console.log(str)
console.log(htmlspecialchars(str))
$('#scriptCode').html(htmlspecialchars(str));
</script>
字符串js编码转换成实体html编码的方法(防范XSS攻击)的更多相关文章
- 生成二维码 加密解密类 TABLE转换成实体、TABLE转换成实体集合(可转换成对象和值类型) COOKIE帮助类 数据类型转换 截取字符串 根据IP获取地点 生成随机字符 UNIX时间转换为DATETIME\DATETIME转换为UNIXTIME 是否包含中文 生成秘钥方式之一 计算某一年 某一周 的起始时间和结束时间
生成二维码 /// <summary>/// 生成二维码/// </summary>public static class QRcodeUtils{private static ...
- 转换编码,将Unicode编码转换成可以浏览的utf-8编码
//转换编码,将Unicode编码转换成可以浏览的utf-8编码 public function unicodeDecode($name) { $pattern = '/([\w]+)|(\\\u([ ...
- JavaScript进阶(四)js字符串转换成数字的三种方法
js字符串转换成数字的三种方法 在js读取文本框或者其它表单数据的时候获得的值是字符串类型的,例如两个文本框a和b,如果获得a的value值为11,b的value值为9 ,那么a.value要小于b. ...
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
js replace 全局替换 js 的replace 默认替换只替换第一个匹配的字符,如果字符串有超过两个以上的对应字符就无法进行替换,这时候就要进行一点操作,进行全部替换. <scrip ...
- C# 将DataTable数据源转换成实体类
using System; using System.Collections.Generic; using System.Data; using System.Reflection; /// < ...
- C# DataTable转换成实体列表 与 实体列表转换成DataTable
/// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T"&g ...
- sql hibernate查询转换成实体或对应的VO Transformers
sql查询转换成实体或对应的VO Transformers //addScalar("id") 默认查询出来的id是全部大写的(sql起别名也无效,所以使用.addScalar(& ...
- hibernate查询部分字段转换成实体bean
//hibernate查询部分字段转换成实体bean /** * 查询线路信息 */ @Override public List<Line> getSimpleLineListByTj(M ...
- How to cast List<Object> to List<MyClass> Object集合转换成实体集合
List<Object> list = getList(); return (List<Customer>) list; Compiler says: cannot cast ...
随机推荐
- 几种filter的比较
需要整理 Gaussian filter https://en.wikipedia.org/wiki/Gaussian_filter Laplacian/Laplacian of Gaussian h ...
- Wireshark图解教程
Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据,并为用户提供关于网络和上层协议的各种信息.与很多其他网络工具一样,Wireshark也使用pcap network ...
- MinStack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- View 以Diaglog 方式展示
OutAdmissionBookingEditor admissionBookingEditor = this.LayoutManager.OpenDialog<OutAdmissionBook ...
- WPF 的datagrid 列名中没有显示下划线是怎么回事?
引用:http://www.cnblogs.com/sqzhuyi/archive/2011/07/26/sqlserverclient.html#2831497 功能介绍:输入查询语句,显示列表 出 ...
- ASP lable标签显示过长,自动换行。
<asp:Label ID="lab_BeforPostR" runat="server" CssClass="labSty" Wid ...
- 3D立体显示大屏幕拼接视频墙系统解决方案【转】
http://shop.souvr.com/thread-123416-1-1.html 随着3D立体视像.全息影像等技术不断取得突破性进展,国内外越来越多的公司投身3D显示领域,产品层出不穷.3D技 ...
- Snippet: align a TextView around an image
A few weeks ago I discovered the Spans on Android,after reading the wonderful post by Flavien Lauren ...
- 在 Visual Studio 中打开编辑 cshtml 文件时出现错误:未能完成该操作 无效指针 的解决方法
第一步:关闭 Visual Studio: 第二步:删除 %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModelCache 下的所有文件: ...
- Servlet3.0学习总结——基于Servlet3.0的文件上传
Servlet3.0学习总结(三)——基于Servlet3.0的文件上传 在Servlet2.5中,我们要实现文件上传功能时,一般都需要借助第三方开源组件,例如Apache的commons-fileu ...