Text Range Search and Replace (IE only)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
Example File From "JavaScript and DHTML Cookbook"
Published by O'Reilly & Associates
Copyright 2003 Danny Goodman
-->
<html>
<head>
<title>Recipe 15.3</title>
<style id="mainStyle" type="text/css">
html {background-color:#cccccc}
body {background-color:#eeeeee; font-family:Tahoma,Arial,Helvetica,sans-serif; font-size:12px;
margin-left:15%; margin-right:15%; border:3px groove darkred; padding:15px}
h1 {text-align:right; font-size:1.5em; font-weight:bold}
h2 {text-align:left; font-size:1.1em; font-weight:bold; text-decoration:underline}
.buttons {margin-top:10px}
</style>
<script type="text/javascript">
// Global range object variable
var rng;
// Return TextRange.findText() third parameter arguments
function getArgs(caseSensitive, wholeWord) {
var isCaseSensitive = (caseSensitive) ? 4 : 0;
var isWholeWord = (wholeWord) ? 2 : 0;
return isCaseSensitive ^ isWholeWord;
}
// Unprompted search and replace
function srBatch(container, search, replace, caseSensitive, wholeWord) {
if (search) {
var args = getArgs(caseSensitive, wholeWord);
rng = document.body.createTextRange();
rng.moveToElementText(container);
clearUndoBuffer();
for (var i = 0; rng.findText(search, 1000000, args); i++) {
rng.text = replace;
pushUndoNew(rng, search, replace);
rng.collapse(false) ;
}
}
}
// Prompted search and replace
function srQuery(container, search, replace, caseSensitive, wholeWord) {
if (search) {
var args = getArgs(caseSensitive, wholeWord);
rng = document.body.createTextRange();
rng.moveToElementText(container);
clearUndoBuffer();
while (rng.findText(search, 10000, args)) {
rng.select();
rng.scrollIntoView();
if (confirm("Replace?")) {
rng.text = replace;
pushUndoNew(rng, search, replace);
}
rng.collapse(false) ;
}
}
}
/****************
UNDO BUFFER
*****************/
// Temporary storage of undo information
var undoObject = {origSearchString:"",newRanges :[]};
// Store original search string and bookmarks of each replaced range
function pushUndoNew(rng, srchString, replString) {
undoObject.origSearchString = srchString;
rng.moveStart("character", -replString.length);
undoObject.newRanges[undoObject.newRanges.length] = rng.getBookmark();
}
// Empty array and search string global
function clearUndoBuffer() {
undoObject.origSearchString = "";
undoObject.newRanges.length = 0;
}
// Perform the undo
function undoReplace() {
if (undoObject.newRanges.length && undoObject.origSearchString) {
for (var i = 0; i < undoObject.newRanges.length; i++) {
rng.moveToBookmark(undoObject.newRanges[i]);
rng.text = undoObject.origSearchString;
}
clearUndoBuffer();
}
}
</script>
<script type="text/javascript">
// process "Search and Replace (with prompt)"
function process1(form, container) {
var search = form.searchString.value
var replace = form.replaceString.value
var caseSensitive = form.caseSensitive.checked
var wholeWord = form.wholeWord.checked;
srQuery(container, search, replace, caseSensitive, wholeWord)
}
// process "Search, Replace (no prompt)"
function process2(form, container) {
var search = form.searchString.value
var replace = form.replaceString.value
var caseSensitive = form.caseSensitive.checked
var wholeWord = form.wholeWord.checked;
srBatch(container, search, replace, caseSensitive, wholeWord)
}
</script>
</head>
<body>
<h1>IE TextRange Search and Replace</h1>
<hr />
<form>
<p>Enter a string to search for in the following text:
<input type="text" name="searchString" size="20" value="Law">
<input type="checkbox" name="caseSensitive">Case-sensitive
<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="process1(this.form, document.body)"></p>
<p><input type="button" value="Search and Replace No Prompt" onClick="process2(this.form, document.body)"></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>
<a name="article2">
<h2>ARTICLE II</h2>
</a>
<p>
A well regulated militia, being necessary to the security of a free state, the right of the people to keep and bear arms, shall not be infringed.
</p>
<a name="article3">
<h2>ARTICLE III</h2>
</a>
<p>
No soldier shall, in time of peace, be quartered in any house, without the consent of the owner, nor in time of war, but in in a manner to be prescribed by law.
</p>
<a name="article4">
<h2>ARTICLE IV</h2>
</a>
<p>
The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.
</p>
<a name="article5">
<h2>ARTICLE V</h2>
</a>
<p>
No person shall be held to answer for a capital, or otherwise infamous crime, unless on a presentment or indictment of a grand jury, except in cases arising in the land or naval forces, or in the militia, when in actual service in time of war or public danger; nor shall any person be subject for the same offense to be twice put in jeopardy of life or limb; nor shall be compelled in any criminal case to be a witness against himself, nor be deprived of life, liberty, or property, without due process of law; nor shall private property be taken for public use, without just compensation.
</p>
<a name="article6">
<h2>ARTICLE VI</h2>
</a>
<p>
In all criminal prosecutions the accused shall enjoy the right to a speedy and public trial, by an impartial jury of the state and district wherein the crime shall have been committed, which district shall have been previously ascertained by law, and to be informed of the nature and cause of the accusation; to be confronted with the witnesses against him; to have compulsory process for obtaining witnesses in his favor, and to have the assistance of counsel for his defense.
</p>
<a name="article7">
<h2>ARTICLE VII</h2>
</a>
<p>
In suits at common law, where the value in controversy shall exceed twenty dollars, the right of trial by jury shall be preserved, and no fact tried by a jury shall be otherwise re-examined in any court of the United States, than according to the rules of the common law.
</p>
<a name="article8">
<h2>ARTICLE VIII</h2>
</a>
<p>
Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted.
</p>
<a name="article9">
<h2>ARTICLE IX</h2>
</a>
<p>
The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
</p>
<a name="article10">
<h2>ARTICLE X</h2>
</a>
<p>
The powers not delegated to the United States by the Constitution, nor prohibited by it to the states, are reserved to the states respectively, or the the people.
</p>
</div>
</body>
</html>
Related examples in the same category