Olá pessoal, Eu estava procurando um script que não encontrei por aqui, visitando o About.com, quando me deparei com um SOS de um(a) náufrago(a) por lá. Como ele ou ela, diz que está com prazo apertado, e ninguém postou nada lá, vou deixar o pedido aqui, traduzido, e com o link da página do Forum. Se alguém der retorno, postarei lá. Testei a página HTML que está lá, e no IE7 só vejo o codedButton, não visualizo o que ele/ela chama de "vertualButton". Como estou sem o FF 3, não posso no momento testar também nele, nem no Opera. Grato e um abraço, Jorge Oliveira - aka joliv ---------------------------------------------------- Pedido de SOS de Nayana4u , no About.com http://forums.about.com/n/pfx/forum.aspx?n...pt&tid=3117 Urgent help from Expert JavaScript progr From: Nayana4u (Nayana4u) Last Visit: Aug-17 To: All Posted: Aug 17 08 12:06 AM Message: 3117.1 (1 of 1) Descrição: Nesta página (abaixo) há dois botões. Um é gerado pelo código HTML, e é visualizado como "codeButton". O outro é gerado por javascript. Ele é visualizado como "vertualButton". A função usada para a geração do botão é "GenarateBut() function" (¹) A função usada para para inserir caracteres na posição do cursor é "insertChar() function". A função é codificada separadamente para uso no Internet Explorer e no Firefox. Precisamos ter ID de área de texto e caracteres como argumento da função. Os botões "vertualButton" (²) e "codedButton" chamam a mesma função com um clique. O Problema: No Internet Explorer Quando eu clico codedButton ele responde como deve. Mas se eu clico virtualButton, mais vezes, ele me dá um erro inesperado. Também, em ambos os navegadores, Firefox e IE, o cursor fica oculto quando eu clico vertualButton. Por favor me ajudem rápido. Eu tenho apenas três semanas para finalizar meu projeto. Obrigado a todos. Nayana Adassuriya NOTA do JOLIV ----------------- (¹) - GenarateBut, não seria GenerateBut? (²) - vertualButton, não seria VirtualButton? Testei a página no IE 7 e apenas o codedButton pode ser visualizado. ------------------------------web page------------------------------ Discription: In this page there are two buttons One is generated by HTML code, it display as “codedButton”. Other one is generated by run time using javascript. That display as “vertualButton”. GenarateBut() function used for that purpose. insertChar() function used for insert character to the cursor position of the text area. In that function separately coded for IE and firefox. We have to give id of the text area and character as arguments of the function. Above both buttons (vertualButton and codedButton) are calling same function at click event. Problem: In IE When I click codedButton it works very fine. But if I click virtualButton, more times it giving unexpected error. And also in both firefox and IE both cursor is hiding when I click the vertualButton. Plz help me soon. I have only another three weeks to finish my project Thank all Nayana adassuriya <html>
<head>
<script type=text/javascript>
function GenarateBut(){
key=document.createElement('input');
key.setAttribute('id', 'butMain');
key.setAttribute('type', 'button');
key.setAttribute('value', 'vertualButton');
key.setAttribute('width', 100);
key.onmousedown = function() { checkCursorPosition('tArea','@'); }
document.getElementById('DIV1').appendChild(key);
}
function countChars(text) {
var numOfChar = 0;
for (var i = 0; i < text.length; i++) {
if (text.charAt(i) != '\r') {
numOfChar++;
}
}
return numOfChar;
}
function insertChar(id,myValue) {
var staPos = 0;
var endPos = 0;
var tArea=document.getElementById('tArea');
if (document.selection) { // IE…
tArea.focus();
var range = document.selection.createRange();
var dupRange = range.duplicate();
dupRange.moveToElementText(tArea);
var selText = range.text;
range.text = '|%|';
var index = dupRange.text.indexOf('|%|');
staPos = countChars((index == -1) ? dupRange.text : dupRange.text.substring(0, index));
endPos = countChars(selText) + staPos;
range.moveStart('character', -3);
range.text = myValue;
//set cursor position
var sel = tArea.createTextRange();
sel.collapse(true);
sel.moveStart('character', staPos+myValue.length);
sel.moveEnd('character', 0);
sel.select();
} else if (tArea.selectionStart || (tArea.selectionStart == '0')) { // Mozilla/Netscape…
staPos = tArea.selectionStart;
endPos = tArea.selectionEnd;
tArea.value = tArea.value.substring(0, staPos)+ myValue+ tArea.value.substring(endPos, tArea.value.length);
tArea.selectionStart = (staPos+myValue.length);
tArea.selectionEnd = (staPos+myValue.length);
}
tArea.focus();
}
</script>
<style type=text/css>
body {
font-size: 1em; font-family: verdana, helvetica, arial, sans-serif; background-color: #4a88be;
}
textarea {
left: 250px; overflow: auto; width: 500px; font-family: "iskoola pota"; position: absolute; top: 30px; height: 100px;
}
#div1 {
left: 400px; width: 50px; position: absolute; top: 600px; height: 50px; background-color: #000000;
}
#butmain {
left: 20px; width: 200px; position: absolute; top: 20px; height: 50px; background-color: #ffffff;
}
</style>
</head>
<body onload="genarateBut()">
<textarea id=tarea name=textarea></textarea>
<div id=div1 style="left: 247px; width: 507px; top: 285px; height: 167px">
<input type="submit" name="submit" value="codedButton" onclick="java script:insertChar('tarea','@')" style="position:absolute; left: 200px; width: 200px; top: 100px; height: 50px">
</div>
</body>
</html>