Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ajax won't display script tag text in textarea box

Status
Not open for further replies.

dezwald

Programmer
Dec 18, 2007
6
CA
hi there,

i have a textarea box that i want to be able to save javascript code into a database. (which i have no problems doing)

my problem is displaying the javascript code/script tags in my textarea after retreiving it from my database via ajax request. it won't show:

<script>blah blah blah</script>

i know that the js code/script tags are being retreived from the database because i can inspect ajax repsonse and see the script tags.

i set the evalScript to false so that it will not evaluate js code and still i see nothing in my text area box.

the only thing that will show is any text outside of the script tags.

any help?
 
You need to escape the data when delivering it to the page so characters such as < and > are turned into their character entity codes such as &lt; and &gt;.

Most server-side frameworks have pre-made functions to do this, so you're better off asking in the forum for your language (e.g. PHP, ASP, etc) to find out how to escape these.

Then, you'd run the escape function when setting the value of your textarea, e.g. (and for no particular server-side language):

Code:
<textarea name="whatever">
   <% echo escapeAsHtml('your code string here'); %>
</textarea>

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
yes this helped and solved my problem

thanks very much
 
An alternative is to enclose the entire string in a CDATA block, so that the XML parser that receives your AJAX reply will not parse this stuff, but treat it as plain text. I have had to do this when delivering javascript and HTML to be stuffed into a string on the client side rather than being evaluated or rendered. The precise construct is

<![CDATA[.......]]>

where the dots represent your actual string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top