Thursday, October 6, 2011

For finding text in webpage javascript

In Internet Explorer 

to copy the content of the webpage use

 txt=document.body.CreateTextRange();

Now we need to search the desired string using findtext method

  Assume string is the desired string to be searched

1)if txt.findtext(string)
    {
 2)       txt.select() ; //will highlight the searched word
        
}
For moving to the next string you have to bookmark the String
we need to move to the end we use


 3)   txt.collapse(false) ;

if we use true in this case it moves to the beginning

And again calling the  above three commands will move to the next word.

If we want to move to the previously searched word we need to follow a technique called BookMarking =


var dd=txt.getBookmark();

Here the  getBookMark saves a opaque string which can be used to move to the previously referrenced
textrange using the

txt.moveToBookmark(dd);
likewise you can save the Bookmark for each searched string and retrieve it back.
 
To search a word in firefox and other browsers we need to use window.find method

To search forward use


window.find("string to search",false,false);
First argument is string to be searched
Second false signifies no need of case sensitivity
Third false signifies forward search

To search Backward use


window.find("string to search",false,true);
First argument is string to be searched
Second false signifies no need of case sensitivity
Third  true signifies backward search.

To get the selection you can use

sel=window.getSelection();
This can also be used separately if you want to select the text highlighted by the visitor.

To copy the selection to a variable use:

range=sel.getRangeAt(0);
To add the range to the selection use

sel.addRange(range);

To remove the selection  from the webPage  you can use

sel.removeAllRanges();

to hide and make visible the textbox javascript



To hide the textbox use


var gg=document.getElementById("txtbox");
gg.style.visibility='hidden';


To make visible the textbox use:
var gg=document.getElementById("txtbox");
gg.style.visibility='visible';