|
Need help with JavaScript "Search and Replace" script
Old duffer, but new to JavaScript.
I'm trying to use Danny Goodwin's "Search and Replace" script; found below.
If you load the script in a page (I use Dreamweaver) you will see two check boxes; "Case-sensitive & Whole words only." I want to REMOVE both of those boxes.
After the boxes are removed I want the searches to ALWAYS be Case-sensitve but NOT Whole words only.
I hope I said that clearly; I'm a little tired.
I can't figure out how to do it. Everything I've tried just kills the whole program.
If you can help me, you most certainly will have my thanks.
Frank
---------------------------------------------------------------------------------------
<!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>
<p> </p>
</div>
</body>
</html>
|