HighDots Forums  

selection text.. esiste qualcosa crossbrowser?

Javascript (Italian) Il linguaggio JavaScript (it.comp.lang.javascript)


Discuss selection text.. esiste qualcosa crossbrowser? in the Javascript (Italian) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
simple:maL
 
Posts: n/a

Default selection text.. esiste qualcosa crossbrowser? - 06-07-2006 , 08:29 AM






mi trovo a dover manipolare via js il testo di una textarea in modo
dinamico, tenendo conto della posizione del cursore e di eventuale testo
selezionato... il tutto per realizzare un piccolo form che permetta
l'inserimento semplificato di testo con bbcode.

ho trovato uno script che sto' utilizzando come base che è il seguente:

# è l'oggetto textarea
# bbcode è lo stile che voglio applicare (es.bold)

function insert_bbcode(v, bbcode){
try {
if (document.selection)
{
var str = document.selection.createRange().text;
v.focus();
var sel = document.selection.createRange();
sel.text = "[" + bbcode + "]" + str + "[/" + bbcode + "]";
return;
}
else if ((typeof v.selectionStart) != 'undefined')
{
var txtarea = v;
var selLength = txtarea.textLength;
var selStart = txtarea.selectionStart;
var selEnd = txtarea.selectionEnd;
var s1 = (txtarea.value).substring(0,selStart);
var s2 = (txtarea.value).substring(selStart, selEnd)
var s3 = (txtarea.value).substring(selEnd, selLength);
txtarea.value = s1 + '[' + bbcode + ']' + s2 + '[/' + bbcode + ']' + s3;
txtarea.focus();
txtarea.selectionStart = s1.length;
txtarea.selectionEnd = s1.length + 5 + s2.length + v.length * 2;
return;
}
else {}
}
catch(e){
}

lo script va cmq implementato perchè devo poter inserire attributi al
bbcode e devo fare verifiche aggiuntive in merito l'area selezionata, ma
la cosa che non funziona ne su IE ne su FF è l'impostazione della
selezione attiva al termine della modifica.
Cioè se seleziono le lettere abc e uso la funzione per applicare il
bold, vorrei che poi rimanesse come area selezionata sempre abc... ma nn
funziona!

suggerimenti?
tnx

simple_maL

Reply With Quote
  #2  
Old   
Agostino De Matteis
 
Posts: n/a

Default Re: selection text.. esiste qualcosa crossbrowser? - 06-07-2006 , 09:28 AM






Il Wed, 07 Jun 2006 14:29:02 +0200, simple:maL ha scritto:

Non l'ho testato, ma prova a modificare in qualcosa del genere:

Quote:
function insert_bbcode(v, bbcode){
try {
if (document.selection)
{
v.focus( v.caretPos );
v.caretPos = document.selection.createRange().duplicate();
tSel = document.selection.createRange().text;
text = "[" + bbcode + "]" + tSel + "[/" + bbcode + "]";
v.caretPos.text = text;
v.caretPos.moveStart( "character", text.length * -1 );
v.caretPos.select();
Quote:
return;
}
else if ((typeof v.selectionStart) != 'undefined')
{
var bPos = v.selectionStart;
var ePos = v.selectionEnd;
var sTop=v.scrollTop;
var myText = (v.value).substring(bPos, ePos);
subst = "[" + bbcode + "]" + myText + "[/" + bbcode + "]";
v.value = v.value.substring(0, bPos) + subst +
v.value.substring(ePos, v.value.length);
v.focus();
var cPos=bPos+(bAdd.length+myText.length+aAdd.length);
v.selectionStart=bPos;
v.selectionEnd=cPos;
v.scrollTop=sTop;
Quote:
return;
}
else {}
}
catch(e){
}

Se non ho fatto errori ad adattare un mio codice al tuo, dovrebbe
funzionare :-)

'bye


Reply With Quote
  #3  
Old   
simple:maL
 
Posts: n/a

Default Re: selection text.. esiste qualcosa crossbrowser? - 06-07-2006 , 10:00 AM



Agostino De Matteis ha scritto:
Quote:
Il Wed, 07 Jun 2006 14:29:02 +0200, simple:maL ha scritto:

Non l'ho testato, ma prova a modificare in qualcosa del genere:
innanzitutto grazie...
su IE va una canna!
su FF invece al termine della modifica mi porta il cursore all'ultimo
carattere... sto' cercando di capire il perchè..
cmq ancora grazie, ottimo tempismo e ottima soluzione

simple_maL


Reply With Quote
  #4  
Old   
simple:maL
 
Posts: n/a

Default Re: selection text.. esiste qualcosa crossbrowser? - 06-07-2006 , 10:24 AM



Agostino De Matteis ha scritto:
Quote:
Il Wed, 07 Jun 2006 14:29:02 +0200, simple:maL ha scritto:
var cPos=bPos+(bAdd.length+myText.length+aAdd.length);
è qui il problema credo...
bAdd e aAdd non esistono mi pare


Reply With Quote
  #5  
Old   
simple:maL
 
Posts: n/a

Default Re: selection text.. esiste qualcosa crossbrowser? - 06-07-2006 , 10:31 AM



Agostino De Matteis ha scritto:
Quote:
Il Wed, 07 Jun 2006 14:29:02 +0200, simple:maL ha scritto:
var cPos=bPos+(bAdd.length+myText.length+aAdd.length);
ho risolto così:
var cPos=bPos+subst.length;

funziona tutto!
grazie ancora
simple_maL


Reply With Quote
  #6  
Old   
Agostino De Matteis
 
Posts: n/a

Default Re: selection text.. esiste qualcosa crossbrowser? - 06-08-2006 , 07:27 AM



Il Wed, 07 Jun 2006 16:31:44 +0200, simple:maL ha scritto:

Quote:
var cPos=bPos+(bAdd.length+myText.length+aAdd.length);

ho risolto così:
var cPos=bPos+subst.length;
Lo dicevo che avrei fatto casini mischiando i due codici :-)

Quote:
funziona tutto!
Ottimo.

Quote:
grazie ancora
Prego.

'bye


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.