To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here

Go Back   IT Management Forum > Intranet Journal > Support Questions & Advice

Support Questions & Advice Get advice and answers to your questions on desktop support, server support and application support.

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-01-2008, 02:12 PM
Indianbear Indianbear is offline
Registered User
 
Join Date: Dec 2008
Posts: 2
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"> &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="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>&nbsp;</p>
</div>
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 12-02-2008, 12:27 PM
Indianbear Indianbear is offline
Registered User
 
Join Date: Dec 2008
Posts: 2
I found the answer. The crisis is over.

Thanks
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 09:48 AM.





Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.0.0