Groups > Browsers > Opera page authoring > Re: works in IE and FF but not in Opera




works in IE and FF but not in Opera

works in IE and FF but not in Opera
Fri, 04 Aug 2006 11:44:23 +020
I use the following JavaScript to put characters into an HTML Input (type=text)
when a 
button is clicked:

function fakeKeyPress(elementID, myCharValue) {
   var myField = document.getElementById(elementID);
	myField.focus();
   //IE,OPERA
   if (document.selection) {
     sel = document.selection.createRange();
     sel.text = myCharValue;
   }
   //FF
   else {
	  if (myField.selectionStart || myField.selectionStart == '0') {
       var startPos = myField.selectionStart;
       var endPos = myField.selectionEnd;
       myField.value = myField.value.substring(0, startPos)
       + myCharValue
       + myField.value.substring(endPos, myField.value.length);
       myField.selectionStart = startPos + 1;
  	    myField.selectionEnd = startPos+ 1;
     } else {
       myField.value += myCharValue;
     }
   }
}

It works fine in IE and FF, and it does put the character to the field in Opera
9.01 (Win 
XP), but then it redraws the entire page (i.e. it calls my onLoad function), and
that 
effectively deletes the content of the inupt.  How can I avoid this redraw?

Thanks,

Post Reply
Re: works in IE and FF but not in Opera
Fri, 04 Aug 2006 13:49:37 +020
This is not such a big problem as I thought since it happens only when I load
the page via 
localhost.  Still, I would like to know how to avoid it even locally.

JAS

James A. Schulz wrote:
> I use the following JavaScript to put characters into an HTML Input 
> (type=text) when a button is clicked:
> 
> function fakeKeyPress(elementID, myCharValue) {
>   var myField = document.getElementById(elementID);
>     myField.focus();
>   //IE,OPERA
>   if (document.selection) {
>     sel = document.selection.createRange();
>     sel.text = myCharValue;
>   }
>   //FF
>   else {
>       if (myField.selectionStart || myField.selectionStart == '0') {
>       var startPos = myField.selectionStart;
>       var endPos = myField.selectionEnd;
>       myField.value = myField.value.substring(0, startPos)
>       + myCharValue
>       + myField.value.substring(endPos, myField.value.length);
>       myField.selectionStart = startPos + 1;
>          myField.selectionEnd = startPos+ 1;
>     } else {
>       myField.value += myCharValue;
>     }
>   }
> }
> 
> It works fine in IE and FF, and it does put the character to the field 
> in Opera 9.01 (Win XP), but then it redraws the entire page (i.e. it 
> calls my onLoad function), and that effectively deletes the content of 
> the inupt.  How can I avoid this redraw?
> 
> Thanks,
> 
Post Reply
Re: works in IE and FF but not in Opera
Mon, 07 Aug 2006 11:29:30 +020
On Fri, 04 Aug 2006 13:49:37 +0200, James A. Schulz  
<jaschulz@wiseware.com> wrote:

> This is not such a big problem as I thought since it happens only when I  
> load the page via localhost.  Still, I would like to know how to avoid  
> it even locally.
>
> JAS
>
> James A. Schulz wrote:
>> I use the following JavaScript to put characters into an HTML Input  
>> (type=text) when a button is clicked:
>>  function fakeKeyPress(elementID, myCharValue) {
>>   var myField = document.getElementById(elementID);
>>     myField.focus();
>>   //IE,OPERA
>>   if (document.selection) {
>>     sel = document.selection.createRange();
>>     sel.text = myCharValue;
>>   }
>>   //FF
>>   else {
>>       if (myField.selectionStart || myField.selectionStart == '0') {
>>       var startPos = myField.selectionStart;
>>       var endPos = myField.selectionEnd;
>>       myField.value = myField.value.substring(0, startPos)
>>       + myCharValue
>>       + myField.value.substring(endPos, myField.value.length);
>>       myField.selectionStart = startPos + 1;
>>          myField.selectionEnd = startPos+ 1;
>>     } else {
>>       myField.value += myCharValue;
>>     }
>>   }
>> }
>>  It works fine in IE and FF, and it does put the character to the field
 
>> in Opera 9.01 (Win XP), but then it redraws the entire page (i.e. it  
>> calls my onLoad function), and that effectively deletes the content of 

>> the inupt.  How can I avoid this redraw?
>>  Thanks,
>>  JAS

I doubt that your javascript is causing the problem. I think it's the way  
Opera handles button elements. I'm constantly rewriting forms with button  
elements to forms with input elements of which the type is button, for the  
very same reason. Not even a "return false;" in the javascript stops
the  
form from being submitted when using a button element. Neither Firefox nor  
Internet Explorer react to buttons this way.

I don't know whether or not this would be a bug. I do think it's annoying.

-- 
Yours,
ΩJr

Post Reply
about | contact