[Javascript] incompatibilité d'un bbcode avec Internet Explorer
Bonsoir à tous :) ,
J'ai un bbcode et il ne fonctionne parfaitement que sous Firefox.
Le fontionnement selon les différent navigateurs
Fonctionne correctement sous Firefox
- Les balises encadrent le texte sélectionné
- Le texte reste sélectionné après l'ajout d'une balise
Problème sous IE
- il met les balises demandées à droite du texte sélectionné
Les codes HTML et Javascript concerné
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <div>
<div onclick="javascript:bbcode('[gras]', '[/gras]');return(false)"></div>
<div onclick="javascript:bbcode('[italic]', '[/italic]');return(false)"></div>
<div onclick="javascript:bbcode('[underline]', '[/underline]');return(false)"></div>
</div>
<div onclick="javascript:bbcode('[gauche]', '[/gauche]');return(false)"></div>
<div onclick="javascript:bbcode('[centre]', '[/centre]');return(false)"></div>
<div onclick="javascript:bbcode('[droite]', '[/droite]');return(false)"></div>
<div onclick="javascript:bbcode('[justifie]', '[/justifie]');return(false)"></div>
</div>
<div>
<div onClick="javascript:bbcode('<a href="http://', '" target="_blank">', '</a>');return(false)"></div>
<div onclick="javascript:code();return(false)"></div>
<div onclick="javascript:code2();return(false)"></div>
</div>
<select name="couleur">
<option selected="selected">Couleur</option>
<option value="rouge" onclick="javascript:bbcode('[rouge]', '[/rouge]');return(false)">rouge</option>
<option value="bleu" onclick="javascript:bbcode('[bleu]', '[/bleu]');return(false)">bleu</option>
</select>
<select name="taille_texte" class="taille_texte">
<option selected="selected">Taille du texte</option>
<option value="petit" onclick="javascript:bbcode('[taille=petit]', '[/taille]');return(false)">Petit</option>
<option value="grand" onclick="javascript:bbcode('[taille=grand]', '[/taille]');return(false)">Grand</option>
</select> |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| function bbcode(bbdebut, bbfin){
var input = window.document.redaction_administration.textarea;
input.focus();
/* pour IE (toujous un cas appart lui )*/
if(typeof document.selection != 'undefined'){
var range = document.selection.createRange().duplicate();
var insText = range.text;
range.text = bbdebut + insText + bbfin;
if(insText.length == 0) range.move('character', -bbfin);
else{
range.moveStart('character', -(insText.length + bbfin.length));
range.moveEnd('character', -bbfin.length);
range.select();
}
}
else if(typeof input.selectionStart != 'undefined'){
var deb_sel = input.value.substring(0, input.selectionStart);
var end_sel = input.value.substring(input.selectionEnd, input.value.length);
var sel = input.value.substring(deb_sel.length, input.value.length-end_sel.length);
input.value = deb_sel + bbdebut + sel + bbfin + end_sel;
//Ajustement de la position du curseur
posStart = deb_sel.length + bbdebut.length;
posEnd = deb_sel.length + bbdebut.length + sel.length;
if(sel.length != 0) input.setSelectionRange(posStart, posEnd);
else input.setSelectionRange(posStart, posStart);
}
/* pour les autres navigateurs comme Netscape... */
else{
var pos;
var re = new RegExp('^[0-9]{0,3}$');
while(!re.test(pos)) pos = prompt("insertion (0.." + input.value.length + ";):", "0");
if(pos > input.value.length) pos = input.value.length;
var insText = prompt("Veuillez taper le texte");
input.value = input.value.substr(0, pos) + bbdebut + insText + bbfin + input.value.substr(pos);
}
}
function smileys(img)
{
window.document.redaction_administration.textarea.value += '' + img + '';
} |
Voilà merci d'avance pour votre aide ;)
NB : Je précise que je ne connais strictement rien au Javascript et que j'ai du reprendre ce code.