0

How can I prevent a script from executing in text boxes, by using jQuery?

Here is my complete problem:

I want to send a message to someone using jQuery; if I entered a script tags like a, href, script, etc. they are not appearing in the other's message. How can I prevent this?

4
  • 3
    Your question makes little sense in its current form. Please try to explain your problem in more detail, providing code where relevant. Commented Mar 17, 2011 at 12:00
  • @jamiec i want to ping some code to my friend ..but the html tags in the text are not appearing to my friend Commented Mar 17, 2011 at 12:02
  • Explain "ping some code" - that could mean a whole bunch of stuff. Commented Mar 17, 2011 at 12:04
  • like ... me :hi james is it my code correct in home page ?? i'm sending the html code i written html input type=text........ ............. but in the receiving end that code not appearing .. Commented Mar 17, 2011 at 12:08

2 Answers 2

0

You need to Encode the text written by the users.

Most common way is doing it server side, for example using ASP you'll need to:

strData = Server.HTMLEncode(strData);

And then if the data contains <b>hello</b> the users will see it literally.

To do it client side: HTML-encoding lost when attribute read from input field

Sign up to request clarification or add additional context in comments.

Comments

0

The question you should be asking is:

How can I render text containing HTML as text instead of HTML?

As your examples are all about HTML, not script.

Since you are using jQuery, add it to the document using $element.text(text) and not $element.html(text).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.