javascript - Insert script tag in textarea with jQuery - Stack Overflow

I'm trying to create a textarea with an embed code, so a user can copy that code.I have two metho

I'm trying to create a textarea with an embed code, so a user can copy that code. I have two methods of embedding (first method is script with paired div), the other one is iframe). I enable the user to select a size of the embedded widget through parameters sent to the server.

The textarea content should be this <script src=""></script>

Usually, that would be set as an encoded HTML (& lt; & gt;) when printed out through the HTML, but in this case I need to set it through Javascript.

So this doesn't work:

$('textarea.embed_code').val('<script src=""></script>');

The closing tag breaks the initial script tag where this line is set.

Any suggestion about how to change the width and height in the script source would be fine too (regex, string replace ...), in which case I'll just print out the textarea content from the HTML.

Any other tag works fine, the issue is just with <script>

I'm trying to create a textarea with an embed code, so a user can copy that code. I have two methods of embedding (first method is script with paired div), the other one is iframe). I enable the user to select a size of the embedded widget through parameters sent to the server.

The textarea content should be this <script src="http://mysite./embed/product_id/width/height"></script>

Usually, that would be set as an encoded HTML (& lt; & gt;) when printed out through the HTML, but in this case I need to set it through Javascript.

So this doesn't work:

$('textarea.embed_code').val('<script src="http://mysite./embed/product_id/width/height"></script>');

The closing tag breaks the initial script tag where this line is set.

Any suggestion about how to change the width and height in the script source would be fine too (regex, string replace ...), in which case I'll just print out the textarea content from the HTML.

Any other tag works fine, the issue is just with <script>

Share asked Mar 25, 2013 at 16:03 ekstraktekstrakt 9101 gold badge12 silver badges28 bronze badges 1
  • 1 Don't use elements designed for user input to display output to users. – Quentin Commented Mar 25, 2013 at 16:04
Add a ment  | 

1 Answer 1

Reset to default 8

To include the string "</script>" as data in a <script> element, escape the /: "<\/script>". This will break the HTML end tag syntax while being equivalent in JavaScript string literal syntax.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744652120a4585962.html

相关推荐

  • javascript - Insert script tag in textarea with jQuery - Stack Overflow

    I'm trying to create a textarea with an embed code, so a user can copy that code.I have two metho

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信