如何使用textarea插入html和javascript代码

我想在我的数据库中插入带有textarea字段的html和JavaScript代码。 问题是代码已插入,但它用反弹替换了所有双重问题。 如何使用textarea插入此代码。 </ p>

&lt; div id =“TA_cdsratingsonlynarrow539”class =“TA_cdsratingsonlynarrow”&gt;&lt; ul id =“Ny62NY”class =“TA_links r04zlbt”&gt;&lt; li id = “jb9xdEapRG”class =“sc9nmfeVX”&gt;&lt; a target =“_ blank”href =“https://www.tripadvisor.com/”&gt;&lt; img src =“https://www.tripadvisor.com/ img / cdsi / img2 / branding / tripadvisor_logo_transp_340x80-18034-2.png“alt =”TripAdvisor“/&gt;&lt; / a&gt;&lt; / li&gt;&lt; / ul&gt;&lt; / div&gt;&lt; script src =” https://www.jscache.com/wejs?wtype=cdsratingsonlynarrow&amp;amp;uniq=539&amp;amp;locationId=671932&amp;amp;lang=en_US&amp;amp;border=false&amp;amp;shadow=false&amp;amp;display_version= 2“&gt;&lt; / script&gt; </ code> </ p>
</ div>

展开原文

原文

I want to insert html and JavaScript code with textarea field in my database. Problem is code has inserted but its replaced all double question with backlash. How to insert this code with textarea.

<div id="TA_cdsratingsonlynarrow539" class="TA_cdsratingsonlynarrow"><ul id="Ny62NY" class="TA_links r04zlbt"><li id="jb9xdEapRG" class="sc9nmfeVX"><a target="_blank" href="https://www.tripadvisor.com/"><img src="https://www.tripadvisor.com/img/cdsi/img2/branding/tripadvisor_logo_transp_340x80-18034-2.png" alt="TripAdvisor"/></a></li></ul></div><script src="https://www.jscache.com/wejs?wtype=cdsratingsonlynarrow&amp;uniq=539&amp;locationId=671932&amp;lang=en_US&amp;border=false&amp;shadow=false&amp;display_version=2"></script>

duanpu1064
duanpu1064 这个答案可能会有所帮助。
大约 3 年之前 回复

1个回答



可以使用textarea输入字段将html和javascript代码插入/注入当前页面。</ p>

考虑一下,你跟着textarea:</ p>

 &lt; div id =“output”&gt;&lt; / div&gt; 
&lt; div style =“margin:25px auto; width; 80%; height:auto;“&gt;
&lt; form id =”myform“name =”myform“&gt;
&lt; textarea id =”mytextarea“name =”mytextarea“cols =”90 “rows =”20“&gt;&lt; / textarea&gt;
&lt; br&gt; &lt; button type =“button”id =“mybutton”&gt;执行&lt; / button&gt;
&lt; / form&gt;
&lt; / div&gt;
</ code> </ pre>

现在,您可以向上面的按钮添加事件监听器,从textarea中提取html和javascript代码并将它们插入到页面中:</ p>

  window.addEventListener('load',function  (){
document.getElementById('mybutton')。addEventListener('click',function(e){
var mytextarea = document.getElementById('mytextarea');
var pattern = /&lt; script&gt;( [^] +?)&lt; \ / script&gt; / mg;
var matches,txt = mytextarea.value;
if(txt){
while(matches = pattern.exec(txt)){
if (匹配[1]){
var tContent = matches [1] .replace(/(
| )+ / gm,'');
var s = document.createElement('script'); \ n s.innerText = tContent;
document.body.appendChild(s);
}
}
txt = txt.replace(pattern,'');
}
if(txt){
document.getElementById('output')。innerHTML = txt;
}
});
});
</ code> </ pre>
</ div>

展开原文

原文

It is possible to insert/inject html and javascript code using textarea input field into the current page.

consider, you've following textarea:

<div id="output"></div>
<div style="margin: 25px auto; width: 80%; height: auto;">
    <form id="myform" name="myform">
        <textarea id="mytextarea" name="mytextarea" cols="90" rows="20"></textarea>
        <br> <button type="button" id="mybutton">execute</button>
    </form>
</div>

Now, you can add event listener to that above button to extract html and javascript code from the textarea and insert them to the page:

    window.addEventListener('load', function () {
        document.getElementById('mybutton').addEventListener('click', function (e) {
            var mytextarea = document.getElementById('mytextarea');
            var pattern = /<script>([^]+?)<\/script>/mg;
            var matches, txt = mytextarea.value;
            if (txt) {
                while (matches = pattern.exec(txt)) {
                    if (matches[1]) {
                        var tContent = matches[1].replace(/(
|)+/gm, '');
                        var s = document.createElement('script');
                        s.innerText = tContent;
                        document.body.appendChild(s);
                    }
                }
                txt = txt.replace(pattern, '');
            }
            if (txt) {
                document.getElementById('output').innerHTML = txt;
            }
        });
    });

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问