Two Search and Replace Approaches (with Undo) : Text HTML « HTML « JavaScript DHTML

JavaScript DHTML
1. Ajax Layer
2. Data Type
3. Date Time
4. Development
5. Document
6. Dojo toolkit
7. Event
8. Event onMethod
9. Ext JS
10. Form Control
11. GUI Components
12. HTML
13. Javascript Collections
14. Javascript Objects
15. Javascript Properties
16. jQuery
17. Language Basics
18. Mochkit
19. Mootools
20. Node Operation
21. Object Oriented
22. Page Components
23. Rico
24. Scriptaculous
25. Security
26. SmartClient
27. Style Layout
28. Table
29. Utilities
30. Window Browser
31. YUI Library
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
JavaScript DHTML » HTML » Text HTML 
Two Search and Replace Approaches (with Undo)

/*
JavaScript Bible, Fourth Edition
by Danny Goodman 

Publisher: John Wiley & Sons CopyRight 2001
ISBN: 0764533428
*/

<HTML>
<HEAD>
<TITLE>TextRange.findText() Method</TITLE>
<SCRIPT LANGUAGE="JavaScript">
// global range var for use with Undo
var rng
// return findText() third parameter arguments
function getArgs(form) {
    var isCaseSensitive = (form.caseSensitive.checked0
    var isWholeWord = (form.wholeWord.checked0
    return isCaseSensitive ^ isWholeWord
}
// prompted search and replace
function sAndR(form) {
    var srchString = form.searchString.value
    var replString = form.replaceString.value
    if (srchString) {
        var args = getArgs(form)
        rng = document.body.createTextRange()
        rng.moveToElementText(rights)
        clearUndoBuffer()
        while (rng.findText(srchString, 10000, args)) {
            rng.select()
            rng.scrollIntoView()
            if (confirm("Replace?")) {
                rng.text = replString
                pushUndoNew(rng, srchString, replString)
            }
            rng.collapse(false)        
        }    
    }
}
// unprompted search and replace with counter
function sAndRCount(form) {
    var srchString = form.searchString.value
    var replString = form.replaceString.value
    var i
    if (srchString) {
        var args = getArgs(form)
        rng = document.body.createTextRange()
        rng.moveToElementText(rights)
        for (i = 0; rng.findText(srchString, 10000, args); i++) {
            rng.text = replString
            pushUndoNew(rng, srchString, replString)
            rng.collapse(false)        
        }
        if (i > 1) {
            clearUndoBuffer()
        }
    }

document.all.counter.innerText = i
}
// BEGIN UNDO BUFFER CODE
// buffer global variables
var newRanges = new Array()
var origSearchString
// store original search string and bookmarks of each replaced range
function pushUndoNew(rng, srchString, replString) {
    origSearchString = srchString
    rng.moveStart("character", -replString.length)
    newRanges[newRanges.length= rng.getBookmark()
}
// empty array and search string global
function clearUndoBuffer() {
    document.all.counter.innerText = "0"
    origSearchString = ""
    newRanges.length = 0
}
// perform the undo
function undoReplace() {
    if (newRanges.length && origSearchString) {
        for (var i = 0; i < newRanges.length; i++) {
            rng.moveToBookmark(newRanges[i])
            rng.text = origSearchString
        }
        document.all.counter.innerText = i
        clearUndoBuffer()
    }
}
</SCRIPT>
</HEAD>
<BODY>
<H1>TextRange.findText() Method</H1>
<HR>
<FORM>
<P>Enter a string to search for in the following text:
<INPUT TYPE="text" NAME="searchString" SIZE=20 VALUE="Law"> &nbsp;
<INPUT TYPE="checkbox" NAME="caseSensitive">Case-sensitive &nbsp;
<INPUT TYPE="checkbox" NAME="wholeWord">Whole words only</P>
<P>Enter a string with which to replace found text:
<INPUT TYPE="text" NAME="replaceString" SIZE=20 VALUE="legislation"></P>
<P><INPUT TYPE="button" VALUE="Search and Replace (with prompt)"
 onClick="sAndR(this.form)"></P>
<P><INPUT TYPE="button" VALUE="Search, Replace, and Count (no prompt)"
 onClick="sAndRCount(this.form)">
<SPAN ID="counter">0</SPAN> items found and replaced.</P>
<P><INPUT TYPE="button" VALUE="Undo Search and Replace"
 onClick="undoReplace()"></P>
</FORM>
<DIV ID="rights">
<A NAME="article1">
<H2>ARTICLE I</H2>
</A>
<P>
Congress shall make no law respecting an establishment of religion,
 or prohibiting the free exercise thereof; or abridging the freedom of speech,
 or of the press; or the right of the people peaceably to assemble, and to
 petition the government for a redress of grievances.
</P>
[The rest of the text is snipped for printing here, but it is on the CD-ROM
 version.]
</DIV>
</BODY>
</HTML>


           
       
Related examples in the same category
1. An example of a scrolling message
2. Animating Text Styles
3. Moving Text
4. Using the TextRectangle Object Properties
5. Exploring the Bounding TextRange Properties
6. compareEndPoints() Method
7. Using the document.selection Object
8. Encipher and Decipher
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.