Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    aggiunta testo in un textarea a metà articolo

    ciao a tutti
    volevo chiedervi una cosa.
    Come si può fare per aggiungere un testo ad un textarea quando dell'altro testo è già posizionato al suo interno ?

    Al momento sono riuscito ad aggiungere del testo attraverso un prompt, ma me lo inserisce alla fine del textarea; ma se io volessi aggiungerlo a metà come dovrei fare ?
    Un po come la pagina dove stò scrivendo in questo momento. Se dò il focus in un punto qualsiasi, lui mi inserisce il testo lì..

    Ho provato a dare un'occhiata al codice che questa pagina utilizza, ma ci sono alcune funzioni che non ne capisco l'utilizzo. Uno di questi è "caretPos".

    Cosa fà ? Ho provato a cercare su google il significato ma non sono stato molto fortunato

    Grazie in anticipo.
    Nulla è impossibile. Il difficile è trovare la soluzione che renda l'impossibile possibile...
    http://www.eternaldream.it

  2. #2
    Utente di HTML.it L'avatar di oronze
    Registrato dal
    Jun 2001
    Messaggi
    3,543
    non penso sia possibile aggiungerlo automaticamente nel mezzo della textarea, dovresti usare un artificio dovresti tagliare il testo all'inizio e salvarlo in una var (txt1) e il testo rimanente in un'altra (txt2) e poi riassemblare il tutto
    txt1 + txt_prompt + txt2

    No ai layout tabellari!

    Insulto libero: http://forum.html.it/forum/showthread.php?s=&postid=12524872#post12524872

  3. #3
    ma come faccio a dire a js dove fermarsi ? quando clicco su un punto qualsiasi del textarea, come faccio a dirgli di stopparsi quando trova il cursore ?
    Nulla è impossibile. Il difficile è trovare la soluzione che renda l'impossibile possibile...
    http://www.eternaldream.it

  4. #4
    Utente di HTML.it L'avatar di oronze
    Registrato dal
    Jun 2001
    Messaggi
    3,543
    Allora, questo non lo so, dovrei studiarmelo io ma, dato che serve a te ti dico dove studiare per capire come hanno fatto
    codice:
    <script type='Text/Javascript' Language='Javascript'>
    	<!--
    
    	var TotSmiles=500;
    	var Browser=1;
    	/*
    	if (navigator.appVersion.indexOf("MSIE")==-1) {
    		Browser=0;
    	}
    	if (navigator.appVersion.indexOf("Windows ")==-1) {
    		Browser=0;
    	}
    	*/
    
    	function openColor()
    	{
    		var Colori;
    		window.open('./Colori.html', Colori, 'toolbar=no,width=400,height=480,resizable=1');
    	}
    
    	function CodePost(txt)
    	{
    		var s=new String(txt);
    		s=s.replace(/'/g,"`");
    		return s;
    	}
    	function openPreview()
    	{
    		var Preview;
    		var IdForum=7381;
    		var IdCartella=2;
    		var IdDiscussione=1905;
    		var sTesto=CodePost(""+document.PostMessaggi.Testo.value);
    		var sTitolo=CodePost(""+document.PostMessaggi.Titolo.value);
    		var siHTML=1;
    		var noSmiles=0;
    		var siFFZ=1;
    		var noURL=0;
    		
    		try {
    		 siHTML=document.PostMessaggi.siHTML.checked;
    		 noSmiles=document.PostMessaggi.noSmiles.checked;
    		 siFFZ=document.PostMessaggi.siFFZ.checked;
    		 noURL=document.PostMessaggi.noURL.checked;
    		}
    		catch(er) {}
    	
    		try {
    		document.Anteprima.f.value=IdForum;
    		document.Anteprima.idc.value=IdCartella;
    		document.Anteprima.idd.value=IdDiscussione;
    		document.Anteprima.msg.value=sTesto;
    		document.Anteprima.titolo.value=sTitolo;
    		document.Anteprima.siHTML.value=siHTML;
    		document.Anteprima.siFFZ.value=siFFZ;
    		document.Anteprima.noSmiles.value=noSmiles;
    		document.Anteprima.noURL.value=noURL;
    		}
    		catch(er) {}
    
    		document.Anteprima.submit();
    	}
    
    	function Fuoco() {document.PostMessaggi.Testo.focus();}
    
    	function InsFFZCode(Opz,tgSx,tgDx,tgDx2) 
    	{
    		try 
    		{
    			Fuoco();
    			var TR = null;
    			var str="";
    	
    			/*
    			if ( window.getSelection ) {
    			TR = window.getSelection();
    			str=new String(TR);
    			}
    			else*/
    			if ( document.selection ) {
    			TR = document.selection.createRange();
    			str=new String(TR.text);
    			}
    			/*
    			else if ( document.getSelection ) {
    			TR = document.getSelection();
    			str=new String(TR);
    			}
    			*/
    			
    			if ( TR != null )
    			{
    				if(Opz!=2) {
    					if(Opz==1 && str.length<1) return(2);
    					if(tgDx2!="") 		TR.text=tgSx+TR.text+tgDx+TR.text+tgDx2;
    					else if(tgDx!="") 	TR.text=tgSx+TR.text+tgDx;
    					else 			TR.text=tgSx+TR.text;
    				}
    				if(Opz==2)	{
    					TR.text=tgSx;
    				}
    				return(0);
    			} 
    			
    		
    			var el=document.getElementById('TestoMessaggio');
    			if (el.setSelectionRange) {
    				var pos=el.selectionStart;
    				str= el.value.substring(el.selectionStart,el.selectionEnd);
    				var start= el.value.substring(0,el.selectionStart) ;
    				var end =el.value.substring(el.selectionEnd,el.value.length);
    				if(Opz!=2) {
    					if(Opz==1 && str.length<1) return(2);
    					if(tgDx2!="") 		
    					{
    						el.value=start + tgSx+str+tgDx+str+tgDx2 + end;
    						pos+=tgSx.length+str.length+tgDx.length+str.length+tgDx2.length;
    					}
    					else if(tgDx!="") 	
    					{
    						el.value=start + tgSx+str+tgDx + end;
    						pos+=tgSx.length+str.length+tgDx.length;
    					}
    					else 				
    					{
    						el.value=start + tgSx+str + end;
    						pos+=tgSx.length+str.length;
    					}
    				}
    				if(Opz==2)	{
    					el.value=start + tgSx + end;
    				}
    				el.setSelectionRange ( pos , pos );
    				return(0);
    			}
    			
    		}
    		catch(err)	{ }
    		return(1);
    	}
    	function InsEmail()
    	{
    		var x=0;
    		if(Browser==1) x=InsFFZCode(1,"","","");
    		if(x!=0 || Browser!=1) {
    			var a=window.prompt("Inserire l`indirizzo email","");
    			if(a==null) { Fuoco(); return; }
    			if(Browser==1)			x=InsFFZCode(2,""+a+"","","");
    			if(x==1 || Browser!=1)	document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+""+a+"";
    		}
    		Fuoco();
    	}
    	function InsImg()
    	{
    		var x=0;
    		if(Browser==1) x=InsFFZCode(1,"[IMG]","[/IMG]","");
    		if(x!=0 || Browser!=1) {
    			var a=window.prompt("Inserire l`indirizzo dell`immagine", "http://");
    			if(a==null) { Fuoco(); return; }
    			if(a.search(/^http:\/\//g)==-1)  a="http://"+a;
    			if(Browser==1)			x=InsFFZCode(2,"[IMG]"+a+"[/IMG]","","");
    			if(x==1 || Browser!=1)	document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+"[IMG]"+a+"[/IMG]";
    		}
    		Fuoco();
    	}
    	function InsLink()
    	{
    		var x=0;
    		if(Browser==1) x=InsFFZCode(1,"","");
    		if(x!=0 || Browser!=1) {
    			var a=window.prompt("Inserire il testo da linkare","");
    			if(a==null) { Fuoco(); return; }
    			var b=window.prompt("Inserire l`indirizzo a cui collegare il testo: "+a,"http://");
    			if(b==null) { Fuoco(); return; }
    			if(b.search(/^www\./g)!=-1)  b="http://"+b;
    			b=b.replace(/[\[\]]/gi,"");
    			if(Browser==1)			x=InsFFZCode(2,""+a+"","","");
    			if(x==1 || Browser!=1)	document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+""+a+"";
    		}
    		Fuoco();
    	}
    	function InsTag(tag)
    	{
    		var x=0;
    		if(Browser==1) x=InsFFZCode(3,"["+tag+"]","[/"+tag+"]","");
    		if(x!=0 || Browser!=1) {
    			document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+"["+tag+"][/"+tag+"]";
    			Fuoco();
    		}
    	}
    	
    	function InsTagEdit(tag) {
    		var x=0;
    		if(Browser==1) {
    			Fuoco();
    			var TR, str;
    			if ( document.selection ) {
    			TR = document.selection.createRange();
    			}
    			if(TR!=null)
    			{
    				var pos=TR.selectionStart;
    				str=new String(TR.text);
    				str=str.replace(/\[CENTER\]/,"");
    				str=str.replace(/\[\/CENTER\]/,"");
    				str=str.replace(/\[RIGHT\]/,"");
    				str=str.replace(/\[\/RIGHT\]/,"");
    				TR.text=str;
    			}
    			else
    			{
    			 var el=document.getElementById('TestoMessaggio');
    			 if (el.setSelectionRange) {
    				var pos=el.selectionStart;
    				str= el.value.substring(el.selectionStart,el.selectionEnd);
    				var start= el.value.substring(0,el.selectionStart) ;
    				var end =el.value.substring(el.selectionEnd,el.value.length);
    				str=str.replace(/\[CENTER\]/,"");
    				str=str.replace(/\[\/CENTER\]/,"");
    				str=str.replace(/\[RIGHT\]/,"");
    				str=str.replace(/\[\/RIGHT\]/,"");
    				el.value=start+str+end;
    				el.setSelectionRange ( pos , pos+str.length );
    			 }
    			}
    			if(tag=="LEFT")	return;
    			x=InsFFZCode(3,"["+tag+"]","[/"+tag+"]","");
    		}
    		if(tag=="LEFT")	return;
    		if(x!=0 || Browser!=1) {
    			document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+"["+tag+"][/"+tag+"]";
    			Fuoco();
    		}
    	}
    	
    	function InsDim()
    	{
    		var dim=document.PostMessaggi.fontSize.value;
    		if(dim=="") return;
    		var x=0;
    		if(Browser==1) x=InsFFZCode(3,"[DIM]"+dim+"[=DIM]","[/DIM]","");
    		if(x!=0 || Browser!=1) {
    			document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+"[DIM]"+dim+"[=DIM][/DIM]";
    			Fuoco();
    		}
    	}
    	function InsFont()
    	{
    		var face=document.PostMessaggi.fontFace.value;
    		if(face=="") return;
    		var x=0;
    		if(Browser==1) x=InsFFZCode(3,"[FONT]"+face+"[=FONT]","[/FONT]","");
    		if(x!=0 || Browser!=1) {
    			document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+"[FONT]"+face+"[=FONT][/FONT]";
    			Fuoco();
    		}
    	}
    	function InsColor()
    	{
    		var col=document.PostMessaggi.txtColor.value;
    		if(col=="") return;
    		var x=0;
    		if(Browser==1) x=InsFFZCode(3,"[COLORE]"+col+"[=COLORE]","[/COLORE]","");
    		if(x!=0 || Browser!=1) {
    			document.PostMessaggi.Testo.value=document.PostMessaggi.Testo.value+"[COLORE]"+col+"[=COLORE][/COLORE]";
    			Fuoco();
    		}
    	}
    	function InsTesto()
    	{
    		/*
    		var sel = document.selection;
    		if (sel!=null) {
    		    var rng = sel.createRange();
    		    if(rng!=null) rng.pasteHTML("Selezione");
    		}*/
    		
    		var a=window.prompt("Inserire il testo da aggiungere", "");
    		var b=a;
    		if(a==null) { Fuoco(); return; }
    		if(document.PostMessaggi.chkBold.checked==true) b="[G]"+b+"[/G]";
    		if(document.PostMessaggi.chkCors.checked==true) b="[C]"+b+"[/C]";
    		if(document.PostMessaggi.chkUnde.checked==true) b="[S]"+b+"[/S]";
    		if(document.PostMessaggi.txtColor.value!="") b="[COLORE]"+document.PostMessaggi.txtColor.value+"[=COLORE]"+b+"[/COLORE]";
    		if(document.PostMessaggi.fontSize.value!="") b="[DIM]"+document.PostMessaggi.fontSize.value+"[=DIM]"+b+"[/DIM]";
    		
    		document.PostMessaggi.Testo.value+=b;
    		Fuoco();
    	}
    	function InsSmiles(code)
    	{
    		var x=0;
    		if(Browser==1) x=InsFFZCode(3," "+code+" ","","");
    		if(x!=0 || Browser!=1) {
    			document.PostMessaggi.Testo.value+=" "+code+" ";
    		}
    		Fuoco();
    	}
    	function setcolor() 
    	{
    		try { document.all.cellColor.bgColor=document.all.txtColor.value; }
    		catch(err) {}
    	}
    	function chkExtra()
    	{
    		var n=0;
    		
    		var aTitle=new Array("","Sondaggio: ","FF4: ","Briscola: ","Mastermind: ");
    		var str=new String(document.PostMessaggi.Titolo.value);
    		str=str.replace(aTitle[1],"");
    		str=str.replace(aTitle[2],"");
    		str=str.replace(aTitle[3],"");
    		str=str.replace(aTitle[4],"");
    		document.PostMessaggi.Titolo.value=''+aTitle[n]+str;
    	}
    	function DisabilitaInsert() {
    		document.PostMessaggi.SubmitInsert.disabled=true;
    		setInterval("AbilitaInsert()", 20000);
    	}
    	function AbilitaInsert() {
    		document.PostMessaggi.SubmitInsert.disabled=false;
    	}
    	function CheckChar(ev) {
    		var s=new String(document.PostMessaggi.TestoMessaggio.value);
    		if(s.length>65536) { 
    			alert("Il massimo numero di caratteri che è possibile inserire è 65536.\n\n"+s.length+" caratteri utilizzati\n");
    			if(window.event) window.event.returnValue= false;
    			else if(ev && ev.preventDefault) ev.preventDefault();
    			else if(ev && ev.stopPropagation) ev.stopPropagation();
    			else return false;
    		}
    		//s=s.substr(0,65536);
    		//document.PostMessaggi.TestoMessaggio.value=s;
    		return true;
    	}
    	function ChkSmiles() {
    		var arrSmiles=new Array();
    		var Testo=new String(document.PostMessaggi.Testo.value);
    				arrSmiles[0]=":)";
    		arrSmiles[1]=":(";
    		arrSmiles[2]=":D";
    		arrSmiles[3]=";)";
    		arrSmiles[4]="8)";
    		arrSmiles[5]=":p";
    		arrSmiles[6]=">:";
    		arrSmiles[7]=":?:";
    		arrSmiles[8]=":lol:";
    		arrSmiles[9]=":cry:";
    		arrSmiles[10]=":evil:";
    
    		var re,m,i,tot=0,z;
    		for(i=0;i<arrSmiles.length;i++) {
    			z=new String(arrSmiles[i]);
    			if(z=="" || z==" ") continue;
    			z=z.replace(/\\/g,"\\\\");
    			z=z.replace(/\)/g,"\\)");
    			z=z.replace(/\(/g,"\\(");
    			z=z.replace(/\]/g,"\\]");
    			z=z.replace(/\[/g,"\\[");
    			z=z.replace(/\^/g,"\\^");
    			z=z.replace(/\$/g,"\\$");
    			z=z.replace(/\?/g,"\\?");
    			z=z.replace(/\|/g,"\\|");
    			z=z.replace(/\./g,"\\.");
    			z=z.replace(/\+/g,"\\+");
    			z=z.replace(/\*/g,"\\*");
    			re=new RegExp(z,"g");
    			if(Testo.search(re)==-1) continue;
    			m=Testo.match(re);
    			tot+=m.length;
    		}
    		return(tot);
    	}
    	function StartSubmit(ev) {
    		var x=ChkSmiles();
    		if(x>TotSmiles) {
    			alert("Il Numero massimo di smiles che si possono inserire è: "+TotSmiles+"\n\nNel messaggio sono presenti: "+x+" Smiles");
    			if(window.event) window.event.returnValue= false;
    			else if(ev && ev.preventDefault) ev.preventDefault();
    			else if(ev && ev.stopPropagation) ev.stopPropagation();
    			return false;
    		}
    		if( CheckChar(ev) == false) {
    			if(window.event) window.event.returnValue= false;
    			else if(ev && ev.preventDefault) ev.preventDefault();
    			else if(ev && ev.stopPropagation) ev.stopPropagation();
    			return false;
    		}
    		DisabilitaInsert();
    	}
    	// -->
    	</script>
    è il js del forum snitz immagino, cmq, per vedere come funziona vai qui http://www.freeforumzone.com/viewforum.aspx?f=7381 e se vai nella sezione testi e spartiti puoi inserire post senza essere registrato (ovviamente non inserirne inutilmente) ma puoi provare la funzione per capire se è veramente quello che ti serve!

    No ai layout tabellari!

    Insulto libero: http://forum.html.it/forum/showthread.php?s=&postid=12524872#post12524872

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.