pigeon11
Messages postés16Date d'inscriptionsamedi 21 mars 2009StatutMembreDernière intervention 4 mai 2009
-
28 avril 2009 à 11:48
ggtry
Messages postés417Date d'inscriptionvendredi 13 juin 2008StatutMembreDernière intervention21 juillet 2010
-
4 mai 2009 à 19:47
Bonjour,
J'ai copié le code suivant dans un fichier .html, pour ajouter du texte à la position du curseur dans un textarea mais ça ne marche pas.
Je n'ai aucune erreur, mais quand je clique sur le bouton ajouter aucun texte ne s'ajoute dans le textearea.
<HTML>
<HEAD>
<TITLE>Insert</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function storeCaret (textEl)
{
if (textEl.createTextRange)
textEl.caretPos = document.selection.createRange().duplicate();
}
function insertAtCaret (textEl, text)
{
if (textEl.createTextRange && textEl.caretPos)
{
var caretPos = textEl.caretPos;caretPos.text caretPos.text.charAt(caretPos.text.length - 1) ' ' ? text + ' ' : text;
}
else
textEl.value = text;
}
</SCRIPT>
</HEAD>
<FORM>
<textarea rows="5" name="zonetexte" cols="20"ONSELECT="storeCaret(this);"ONCLICK="storeCaret(this);"ONKEYUP="storeCaret(this);"> C'est le début du texte.</textarea>
ggtry
Messages postés417Date d'inscriptionvendredi 13 juin 2008StatutMembreDernière intervention21 juillet 20101 29 avril 2009 à 23:10
Bonsoir,
Tu aurais dû poster ta question dans le forum javascript : c'est plus adapté.
Je ne sais pas pourquoi rien ne se passe : mais je ne crois pas que createtextrange puisse fonctionner sous firefox.
Si ton problème se pose sous IE, donnes-tu le js que tu utilises, ou celui de ta source ? Parce que, sauf si le js est désactivé chez toi, le code marche bien sous IE 7. Ou alors, si tu l'as adapté, c'est ton adaptation qui peut poser problème (nb. en général, pour faire un débogage javascript, y compris sous visual studio, tu dois activer le débogage des scripts dans Internet Explorer...)
Une suggestion de code, en repartant du tiens, pour le js, serait (en reprenant ici un code trouvé sur le net, pour firefox, que j'adapte seulement, et qui devrait marcher tant pour IE que pour firefox) :
var toto;
var begin;
var end;
function storeCaret (textEl)
{
if (textEl.caretPos != "undefined" && textEl.createTextRange)
{
textEl.caretPos = document.selection.createRange().duplicate();
toto=textEl.caretPos;
}
else
{
begin = textEl.value.substr(0, textEl.selectionStart);
end = textEl.value.substr(textEl.selectionEnd);
toto = begin + selection + end;
}
}
function insertAtCaret (textEl, text)
{
if (textEl.createTextRange && textEl.caretPos)
{
var caretPos = textEl.caretPos;caretPos.text caretPos.text.charAt(caretPos.text.length - 1) ' ' ? text + ' ' : text;
}
else
{
textEl.value = begin + text + end;
}
}
ggtry
Messages postés417Date d'inscriptionvendredi 13 juin 2008StatutMembreDernière intervention21 juillet 20101 4 mai 2009 à 19:47
Bonsoir,
Oui, c'est normal. Le js donné ci-dessus est minimal et assez mauvais (et d'ailleurs la variable toto n'y sert à rien...)
Tu peux tester par exemple si begin ou end sont undefined ou non :
function insertAtCaret (textEl, text)
{
if (textEl.caretPos && textEl.createTextRange)
{
var caretPos = textEl.caretPos;caretPos.text caretPos.text.charAt(caretPos.text.length - 1) ' ' ? text + ' ' : text;
}
else if (begin != undefined)
{
textEl.value = begin + text + end;
}
}