473,473 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Javascript Highlighter

Newbie question:
How can I use a TextRange object to search for text, and then
highlight it?
The enclosed example works, until you dismiss the alert (done just to
show the results). I would rather the text stayed selected! I guess it
is because the TextRange object r goes out of scope? or summat?

Any ideas?

Cheers,

Simon

<HTML>
<BODY>
Leonardo da Vinci was one of the great masters of the High
Renaissance, especially in painting, sculpture, architecture,
engineering, and science.
</BODY>
<SCRIPT>
var r;
function findInPage(str) {

r = document.body.createTextRange();
var txt = r.text;

var matches = txt.toString().match(new RegExp(str, "g"));
matches = matches ? matches.length : 0;

for (i = 0; i <= matches -1; i++) {
r.findText(str);
r.select();
var sr = document.selection.createRange();
sr.pasteHTML( "<font style='background:red' id='highlightedText'>"
+ str + "</font>");
}
alert(r);
}

</script>

<Form name="search" onSubmit="findInPage(this.string.value);">
<Font size="3"><input name="string" type="text" size="15"
value="the"></font>
<input type="submit" value="Find">
</form>
</HTML>
Jul 20 '05 #1
2 3362
Look here:
http://home.student.utwente.nl/r.g.s...er/dhtml/edit/
"Simon" <si**************@hotmail.com> wrote in message
news:fb**************************@posting.google.c om...
Newbie question:
How can I use a TextRange object to search for text, and then
highlight it?
The enclosed example works, until you dismiss the alert (done just to
show the results). I would rather the text stayed selected! I guess it
is because the TextRange object r goes out of scope? or summat?

Any ideas?

Cheers,

Simon

<HTML>
<BODY>
Leonardo da Vinci was one of the great masters of the High
Renaissance, especially in painting, sculpture, architecture,
engineering, and science.
</BODY>
<SCRIPT>
var r;
function findInPage(str) {

r = document.body.createTextRange();
var txt = r.text;

var matches = txt.toString().match(new RegExp(str, "g"));
matches = matches ? matches.length : 0;

for (i = 0; i <= matches -1; i++) {
r.findText(str);
r.select();
var sr = document.selection.createRange();
sr.pasteHTML( "<font style='background:red' id='highlightedText'>"
+ str + "</font>");
}
alert(r);
}

</script>

<Form name="search" onSubmit="findInPage(this.string.value);">
<Font size="3"><input name="string" type="text" size="15"
value="the"></font>
<input type="submit" value="Find">
</form>
</HTML>

Jul 20 '05 #2
Followed the link(s), saw some nice screen shots, but no code! Am I
missing something?

Simon
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tim Tyler | last post by:
I've released the first proper version of my PHP/HTML syntax highlighter. This program takes HTML from a URL or a file, syntax highlights it and produces a web page containing the results. The...
2
by: Mr.Clean | last post by:
I am working on modifying a syntax highlighter written in javascript and it uses several regexes. I need to add a language to the avail highlighters and need the following regexes modified to...
4
by: Tim Conner | last post by:
Hi, I am writing an application in C# that needs a memo control with the capability of highlight certain syntax of programming languages, very similar to what the Vs.Net does, or any language...
5
by: kichelchen | last post by:
Hi all, Does anybody know, if there is somewhere an editor with highlighter functions to implement in an existing cms with mysql backend? It should be easy to use/install :) Has anyone an idea?...
2
by: Ludwig | last post by:
Hi, thanks to the insight that Kevin Spencer gave me concerning my regular expressions question from a few days ago, I managed to create a first version of my (free) Highlighter RichTextBox...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.