I have Created a simple js function to search circularly in a textarea.
I have attached the js file and the html file.
When I tested this with Firefox and Chrome it works just gr8.
But with IE it fails and gives no error but invalid output.
The main functions used in the JS file Are :
Code:
var thismatch=0
var last=0
var oldPhrase
var oldTextValue
function setSelectionRange(input, selectionStart, selectionEnd)
{
if (input.setSelectionRange)
{
input.setSelectionRange(selectionStart, selectionEnd);
}
else if (input.createTextRange)
{
var range = input.createTextRange();
range.collapse(true);
range.moveEnd("character", selectionEnd);
range.moveStart("character", selectionStart);
range.select();
}
input.focus();
}
function showMatch(TXT, phrase)
{
var lengthTrimmed=0
if (thismatch!=0)
{
if(oldPhrase!=phrase && olTextValue!=TXT.value)
{
thismatch=0;
oldPhrase=phrase;
olTextValue=TXT.value;
//alert("unequal");
}
}
else
{
oldPhrase=phrase;
olTextValue=TXT.value;
thismatch=0;
}
thismatch++;
var t= TXT.value;
phrase=phrase.toUpperCase();
t=t.toUpperCase();
if(t.search(phrase)<0)
return
for(i=1;i<thismatch;i++)
{
if(t.search(phrase)>=0)
{
lengthTrimmed+=t.search(phrase)+String(phrase).length;
t=t.slice(t.search(phrase)+String(phrase).length);
}
}
if(t.search(phrase)<0)
{
thismatch=0
lengthTrimmed=0
t= TXT.value;
t=t.toUpperCase();
//alert("not found");
}
//alert(String(phrase).length+ " " +String(t).length)
var lp = t.search(phrase);
//alert(t + " search=" + lp + "ThisMatch=" + thismatch)
setSelectionRange(TXT,lp+lengthTrimmed,lp+String(phrase).length+lengthTrimmed);
}
function showMatchPrevious(TXT, phrase)
{
var lengthTrimmed=0
if (thismatch!=0)
{
if(oldPhrase!=phrase && olTextValue!=TXT.value)
{
thismatch=0;
oldPhrase=phrase;
olTextValue=TXT.value;
//alert("unequal");
}
else if(thismatch>1)
{
thismatch--;
thismatch--;
}
else
{
var text=TXT.value;
thismatch=0;
while(text.search(phrase)>=0)
{
text=text.slice(text.search(phrase)+String(phrase).length);
thismatch++;
}
if(thismatch>0)
thismatch--;
//alert(" ok thismatch=" + thismatch);
}
}
else
{
oldPhrase=phrase;
olTextValue=TXT.value;
thismatch=0;
}
thismatch++;
var t= TXT.value;
phrase=phrase.toUpperCase();
t=t.toUpperCase();
if(t.search(phrase)<0)
return
for(i=1;i<thismatch;i++)
{
if(t.search(phrase)>=0)
{
lengthTrimmed+=t.search(phrase)+String(phrase).length;
t=t.slice(t.search(phrase)+String(phrase).length);
}
}
if(t.search(phrase)<0)
{
thismatch=0
lengthTrimmed=0
t= TXT.value;
t=t.toUpperCase();
//alert("not found");
}
//alert(String(phrase).length+ " " +String(t).length)
var lp = t.search(phrase);
//alert(t + " search=" + lp + "ThisMatch=" + thismatch)
setSelectionRange(TXT,lp+lengthTrimmed,lp+String(phrase).length+lengthTrimmed);
}