简体   繁体   English

通过javascript注入HTML脚本

[英]HTML script injection via javascript

I have a text field where user can fill href tag script which I have to show as a link later on. 我有一个文本字段,用户可以在其中填充href标签脚本,以后必须将其显示为链接。 But your can enter some malicious script like alert message and more like that which will cause execution on unwanted script at the point where I use that text field value to display. 但是您可以输入一些恶意脚本,例如警报消息,以及更多类似的脚本,这些脚本将在我使用该文本字段值显示时在不需要的脚本上执行。 My question is that how can I restrict user to only enter href tag releated entry in text field and restrict other script to enter. 我的问题是,如何限制用户在文本字段中仅输入href标签相关条目,并限制其他脚本进入。 Thanks In advance. 提前致谢。

use regular expression 使用正则表达式

var urlmatch= /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/;
var textboxValue = textbox.value;
if(textboxValue.match(urlmatch)){
// return true
} else {

// return false;
}
function strip(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}

U need to check whether string is url , if its url accept else reject .. the below links will help ur cause. 您需要检查字符串是否为url,如果它的url接受否则else ..以下链接将对您有所帮助。 How to detect whether a string is in URL format using javascript? 如何使用JavaScript检测字符串是否为URL格式? check if string contains url anywhere in string using javascript 使用JavaScript检查字符串是否在字符串中的任何地方包含url

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

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