Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2007, 03:18 AM   PM User | #1
binaryWeapon
Regular Coder

 
Join Date: Sep 2007
Location: AZ, USA
Posts: 685
Thanks: 6
Thanked 46 Times in 46 Posts
binaryWeapon is on a distinguished road
JavaScript Searching

Are there any methods for searching on the client side w/ javascript? Even if its just searching a variable for a certain string. Are there any methods of doing this?
binaryWeapon is offline   Reply With Quote
Old 11-20-2007, 07:53 AM   PM User | #2
Fang
Regular Coder

 
Join Date: Jun 2004
Posts: 495
Thanks: 0
Thanked 82 Times in 80 Posts
Fang is on a distinguished road
This will find and highlight text in the document:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>highlight</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">
function find(searchTerm){
var contents=document.body.innerHTML;
var idx=contents.search(searchTerm);
if(idx) {
    contents=contents.replace(searchTerm,'<span id="highlight">'+searchTerm+'</span>');
    document.body.innerHTML=contents;
    document.getElementById('highlight').scrollIntoView();
    }
}
</script>

<style type="text/css">
#highlight {background:yellow;}
</style>

</head>
<body>
<label>Find on this page: <input type="text" id="search"></label><button type="button" onclick="find(document.getElementById('search').value);">Highlight it</button>
<h1>Nirafo</h1>
<p>Aphasm escit ra apovum ecripsat. Quorepl alist osmephum ime itasmotis to eleh. Quadeth est. Coticreus oce ro omuf ari trolodeus est. Nosm est. Thihopsiem, minus beh brofiprue teranis. </p>
<h2>Smopada</h2>
<h2 onclick="alert('hi');">Edrecram Ol Quorobucranis</h2>
<h3>Funap</h3>
<p>Ohusch ifemes, presn mapliant ple pupriem nalaho. Acrutra son cro teton hi prenaquid est. Ocophiant asi pso bratetriem eta cragasch est. Volespes in aplasnem usnerhant. Gerociant ina quidu tefum ag hiphue est. </p>
<p>Trog crin maquor on crebatuer tes efascranis. Ehisc pibisnam snatofas, ic smot pel vavetont. Drup nicrus quido edesmiem plarodonis. Ugepsiant sosch log fathon atahotis. Stim dabr quorimapseus vi dathat ufo lequadat. Quoreh muspeus quidicusp, bro psopl tutist ropinuer. Schops tremorh, otusm lusagore dro aquorospore pluvusmont. Socelum in rhetapa tumotis. Tof, vufucrue ul uquadicrum ulovant. Smotr ruquidi ipreprant, an ugist popupr mucanis. Acron osmac ipeh vav iloquader quadi govedas. Das mipacras, scet roteprore tin tabesch opod. Escreh quadot dipapam nan hubo bre etomas. Sorhiant, vasta quoro opraquoriant meriplotis. </p>
<p>Apoquid equoraphes mo buthi apretrotis. Fasm ohesmus, phud planerhes pe mafothius itohonis. Brigiquorum in imischius vicrat. Smubr est. Orhan ipsev tigigue ana omosmer ide tacront. </p>
</body>
</html>
Fang is offline   Reply With Quote
Old 11-20-2007, 07:55 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You can use regular expressions to search for a sub-string within a string:-

E.g:-

var str = "The quick brown fox jumps over the lazy dog";
x = str.match (/(fox)/gi);
alert (x);

You can test your regular expressions at: http://www.ogauge.co.uk/regextester.html


Fang: something is wrong with your version of this script. A working version can be found at:-
http://www.codingforums.com/showthread.php?t=122841

Last edited by Philip M; 11-20-2007 at 09:27 AM.. Reason: Tried Fang's script - does not work!
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:06 PM.


Advertisement
Log in to turn off these ads.