codice:
<html>
<head>
<title>Anteprima link</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<style>
#preview{font-family:verdana, arial; font-size:12px; width:300px; background-color:#ffffff; color:#000000F; overflow:auto;}
</style>
<script>
// inizio htmlEntities ->
String.prototype.htmlEntities = function()
{
var chars = new Array ('&','à','á','â','ã','ä','å','æ','ç','è','é',
'ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô',
'õ','ö','ø','ù','ú','û','ü','ý','þ','ÿ','À',
'Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë',
'Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö',
'Ø','Ù','Ú','Û','Ü','Ý','Þ','€','\"','ß','<',
'>','¢','£','¤','¥','¦','§','¨','©','ª','«',
'¬','_','®','¯','°','±','²','³','´','µ','¶',
'·','¸','¹','º','»','¼','½','¾');
var entities = new Array ('amp','agrave','aacute','acirc','atilde','auml','aring',
'aelig','ccedil','egrave','eacute','ecirc','euml','igrave',
'iacute','icirc','iuml','eth','ntilde','ograve','oacute',
'ocirc','otilde','ouml','oslash','ugrave','uacute','ucirc',
'uuml','yacute','thorn','yuml','Agrave','Aacute','Acirc',
'Atilde','Auml','Aring','AElig','Ccedil','Egrave','Eacute',
'Ecirc','Euml','Igrave','Iacute','Icirc','Iuml','ETH','Ntilde',
'Ograve','Oacute','Ocirc','Otilde','Ouml','Oslash','Ugrave',
'Uacute','Ucirc','Uuml','Yacute','THORN','euro','quot','szlig',
'lt','gt','cent','pound','curren','yen','brvbar','sect','uml',
'copy','ordf','laquo','not','shy','reg','macr','deg','plusmn',
'sup2','sup3','acute','micro','para','middot','cedil','sup1',
'ordm','raquo','frac14','frac12','frac34');
newString = this;
for (var i = 0; i < chars.length; i++)
{
myRegExp = new RegExp();
myRegExp.compile(chars[i],'g')
newString = newString.replace (myRegExp, '&' + entities[i] + ';');
}
return newString;
}
// fine htmlEntities ->
function showpreview(){
contenuto_testo=document.getElementById("testo").value;
// trasformo caratteri in htmlEntities
// se si vuol mantenere inserimento HTML commentare la riga
contenuto_testo=contenuto_testo.htmlEntities();
// trasformo a capo in
contenuto_testo=contenuto_testo.replace(/\n/g,"
");
document.getElementById("preview").innerHTML="" + contenuto_testo + "";
}
</script>
<input type="text" id="testo" onKeyUp="showpreview()" value="http://">
Test link:
<div id="preview"></div>
</body>
</html>
Certamente migliorabile.