JavaScript - search a string on page

Asked By ekta Garg
01-Sep-10 12:36 AM
how to search for a particular string on web page using javascript...can anyone help me in coding this..??
need a reply ASAP

Thanks in advance!!
  Anand Malli replied to ekta Garg
01-Sep-10 12:47 AM
Hi Ekta,

Just follow this url,as it talk about what you want and it has demo as well,its quite cool

http://www.tripbase.com/code/highlightTutorial.htm

let me know
thxs

  Shunmuga Nathan replied to ekta Garg
01-Sep-10 12:55 AM
Try the bellow script it will help you

<
SPAN lang=EN>
<P><script type="text/javascript" language="JavaScript" </P>
<P>src="find.js"></P>
<P></script></P>
<P><input type="button" value="Find on this page..." </P>
<P>onclick="show();"></P>
<P></P>
<P>var window_background = "white"; // the color of the pop-up window</P>
<P>var window_border = "blue"; // the border color of pop-up window</P>
<P>var text_color = "black"; // the color of the text in window</P>
<P>var title_color = "white"; // color of window title text</P>
<P>var window_width = 200; // width of window</P>
<P>var window_height = 150; // height of window</P>
<P>var mozilla_opt = 1; // change to 0 to use Netscape and Firefox built-in 
search window</P>
<P>var start_at = 0; // Change to which character you want to start with on the 
page if IE gives an error because of searching in menus</P>
<P>var ie = (document.all)</P>
<P>if (window.find)</P>
<P>var nav = 1; // to detect if netscape or firefox</P>
<P>else </P>
<P>var nav = 0;</P>
<P>var t = 0; // used for timer to move window in IE when scrolling</P>
<P>var sel; // Selection object needed for Firefox</P>
<P>var range; // range object needed for Firefox</P>
<P>if (!ie) </P>
<P>{</P>
<P>document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | 
Event.MOUSEUP);</P>
<P>}</P>
<P>document.onmousedown = MouseDown;</P>
<P>document.onmousemove = MouseMove;</P>
<P>document.onmouseup = MouseUp;</P>
<P>// Temporary variables to hold mouse x-y pos's</P>
<P>var mousex = 0;</P>
<P>var mousey = 0;</P>
<P>if (ie)</P>
<P>{</P>
<P>var txt = document.body.createTextRange();</P>
<P>var bookmark = new Array();</P>
<P>}</P>
<P>var finds = 0;</P>
<P>function findit() </P>
<P>{</P>
<P>var string = document.getElementById('fwtext').value;</P>
<P>findwindow.style.visibility = 'hidden';</P>
<P>if (ie)</P>
<P>{</P>
<P>bookmark[finds] = txt.getBookmark();</P>
<P>if (string) // only call findText if there is a string or IE will have 
error</P>
<P>if (txt.findText(string)) // if found</P>
<P>{ </P>
<P>txt.select();</P>
<P>txt.scrollIntoView();</P>
<P>txt.collapse(false);</P>
<P>test.innerHTML = "Found";</P>
<P>}</P>
<P>else</P>
<P>{</P>
<P>test.innerHTML = "Not found";</P>
<P>finds--;</P>
<P>}</P>
<P>}</P>
<P>else // Netscape or firefox</P>
<P>{</P>
<P>if (finds > 0)</P>
<P>{</P>
<P>sel = window.getSelection(); // get selection</P>
<P>if(sel.rangeCount > 0) sel.removeAllRanges();</P>
<P>sel.addRange(range); </P>
<P>} </P>
<P>if (string != "") </P>
<P>test.innerHTML = window.find(string, false, false);</P>
<P>sel = window.getSelection(); // get selection</P>
<P>range = sel.getRangeAt(0); // get object</P>
<P>}</P>
<P>finds++;</P>
<P>findwindow.style.visibility = 'visible'; </P>
<P></P>
<P>} // end function findit()</P>
<P> </P>
<P>// This function is to find backwards by pressing the Prev button</P>
<P>function findprev()</P>
<P>{</P>
<P>var string = document.getElementById('fwtext').value;</P>
<P>findwindow.style.visibility = 'hidden';</P>
<P>if (ie)</P>
<P>{</P>
<P>if (finds < 2)</P>
<P>{</P>
<P>findwindow.style.visibility = 'visible';</P>
<P>return; </P>
<P>}</P>
<P>finds = finds - 2; // I don't know why I have to go back 2</P>
<P>txt.moveToBookmark(bookmark[finds]);</P>
<P>findit();</P>
<P>}</P>
<P>else // if netscape or firefox</P>
<P>{</P>
<P>if (finds > 0)</P>
<P>{</P>
<P>sel = window.getSelection(); // get selection</P>
<P>if(sel.rangeCount > 0) sel.removeAllRanges();</P>
<P>sel.addRange(range); </P>
<P>}</P>
<P>if (string != "")</P>
<P>test.innerHTML = window.find(string, false, true); </P>
<P>sel = window.getSelection(); // get selection</P>
<P>range = sel.getRangeAt(0); // get object</P>
<P>}</P>
<P></P>
<P>findwindow.style.visibility = 'visible';</P>
<P></P>
<P>} // end findprev()</P>
<P>function checkkey(e)</P>
<P>{ </P>
<P>var keycode;</P>
<P>if (window.event) // if ie</P>
<P>keycode = window.event.keyCode;</P>
<P>else // if Firefox or Netscape</P>
<P>keycode = e.which;</P>
<P>if (keycode == 13) // if ENTER key</P>
<P>{ </P>
<P>if (ie)</P>
<P>document.getElementById('btn').focus();</P>
<P>findit(); // call findit() function (like pressing NEXT) </P>
<P>}</P>
<P>} // end function checkkey()</P>
<P>function show()</P>
<P>{</P>
<P>if (ie || mozilla_opt == 1)</P>
<P>{</P>
<P>var textbox = document.getElementById('fwtext');</P>
<P>findwindow.style.visibility = 'visible';</P>
<P>textbox.focus();</P>
<P>t = setInterval('move_window();', 500); </P>
<P>document.onkeydown = checkkey;</P>
<P>}</P>
<P>else // if netscape or firefox</P>
<P>window.find();</P>
<P>} // end function show()</P>
<P>function hide()</P>
<P>{</P>
<P>findwindow.style.visibility = 'hidden';</P>
<P>clearTimeout(t);</P>
<P>document.onkeydown = null;</P>
<P>} // end function hide()</P>
<P>function resettext()</P>
<P>{</P>
<P>if (ie)</P>
<P>{</P>
<P>txt = document.body.createTextRange();</P>
<P>txt.moveStart("character", start_at);</P>
<P>}</P>
<P>finds = 0;</P>
<P>} // end function reset()</P>
<P>function move_window()</P>
<P>{</P>
<P>fwtop = parseFloat(findwindow.style.top);</P>
<P>fwleft = parseFloat(findwindow.style.left);</P>
<P>fwheight = parseFloat(findwindow.style.height);</P>
<P>if (document.documentElement.scrollTop) // Needed if you use doctype 
loose.htm</P>
<P>current_top = document.documentElement.scrollTop;</P>
<P>else </P>
<P>current_top = document.body.scrollTop;</P>
<P>if (document.documentElement.clientHeight)</P>
<P>{</P>
<P>if (document.documentElement.clientHeight > 
document.body.clientHeight)</P>
<P>current_bottom = document.body.clientHeight + current_top;</P>
<P>else</P>
<P>current_bottom = document.documentElement.clientHeight + current_top;</P>
<P>}</P>
<P>else</P>
<P>current_bottom = document.body.clientHeight + current_top;</P>
<P>if (document.documentElement.scrollLeft) // Needed if you use doctype 
loose.htm</P>
<P>current_left = document.documentElement.scrollLeft;</P>
<P>else </P>
<P>current_left = document.body.scrollLeft;</P>
<P>if (document.documentElement.clientWidth)</P>
<P>{</P>
<P>if (document.documentElement.clientWidth > document.body.clientWidth)</P>
<P>current_right = document.body.clientWidth + current_left;</P>
<P>else</P>
<P>current_right = document.documentElement.clientWidth + current_left;</P>
<P>}</P>
<P>else</P>
<P>current_right = document.body.clientWidth + current_left;</P>
<P>if (fwtop < current_top)</P>
<P>{ </P>
<P>findwindow.style.top = current_top + 'px'; </P>
<P>}</P>
<P>else if (fwtop > current_bottom - fwheight)</P>
<P>{</P>
<P>findwindow.style.top = current_bottom - fwheight + 'px'; </P>
<P>}</P>
<P>if (fwleft < current_left ||</P>
<P>fwleft > current_right)</P>
<P>{</P>
<P>findwindow.style.left = current_left + 'px';</P>
<P>}</P>
<P>} // end function move_window()</P>
<P> </P>
<P>function MouseDown(e) </P>
<P>{</P>
<P>if (over == 1)</P>
<P>DivID = 'findwindow';</P>
<P></P>
<P>if (over)</P>
<P>{ </P>
<P>if (ie) </P>
<P>{</P>
<P>objDiv = document.getElementById(DivID);</P>
<P>objDiv = objDiv.style;</P>
<P>mousex=event.offsetX;</P>
<P>mousey=event.offsetY;</P>
<P>}</P>
<P>else // if Mozilla or Netscape </P>
<P>{</P>
<P>objDiv = document.getElementById(DivID);</P>
<P>mousex=e.layerX;</P>
<P>mousey=e.layerY;</P>
<P>return false;</P>
<P>}</P>
<P>}</P>
<P>}</P>
<P>function MouseMove(e) </P>
<P>{</P>
<P></P>
<P>if (document.documentElement.scrollTop) // Needed if you use doctype 
loose.htm</P>
<P>current_top = document.documentElement.scrollTop;</P>
<P>else </P>
<P>current_top = document.body.scrollTop;</P>
<P>if (document.documentElement.scrollLeft) // Needed if you use doctype 
loose.htm</P>
<P>current_top = document.documentElement.scrollLeft;</P>
<P>else </P>
<P>current_left = document.body.scrollLeft;</P>
<P></P>
<P></P>
<P>if (objDiv) </P>
<P>{</P>
<P>if (ie) </P>
<P>{</P>
<P>objDiv.pixelLeft = event.clientX-mousex + current_left;</P>
<P>objDiv.pixelTop = event.clientY-mousey + current_top;</P>
<P>return false;</P>
<P>}</P>
<P>else // if Mozilla or Netscape</P>
<P>{</P>
<P>objDiv.style.left = (e.pageX-mousex) + 'px';</P>
<P>objDiv.style.top = (e.pageY-mousey) + 'px';</P>
<P>return false;</P>
<P>}</P>
<P>}</P>
<P>} // end function MouseMove(e)</P>
<P>function MouseUp() </P>
<P>{</P>
<P>objDiv = null;</P>
<P>}</P>
<P>document.write('<div id="findwindow" style="position:absolute'</P>
<P>+ ';left: 0px; top: 0px'</P>
<P>+ ';visibility: hidden' </P>
<P>+ ';background-color: ' + window_background</P>
<P>+ ';border: 2px solid ' + window_border</P>
<P>+ ';width: ' + window_width + 'px'</P>
<P>+ ';height: ' + window_height + 'px'</P>
<P>+ ';color: ' + text_color</P>
<P>+ ';padding: 0px'</P>
<P>+ ';font-size: 14px'</P>
<P>+ ';"'</P>
<P>+ '>');</P>
<P>document.write('<div style="text-align: center'</P>
<P>+ ';width: ' + (window_width-20) + 'px'</P>
<P>+ ';cursor: move' // turn mouse arrow to move icon</P>
<P>+ ';color: ' + title_color</P>
<P>+ ';border: 1px solid ' + text_color</P>
<P>+ ';background-color: ' + window_border</P>
<P>+ ';float: left' </P>
<P>+ ';" onmouseover="over=1;" onmouseout="over=0;">'</P>
<P>+ 'Find Window</div>');</P>
<P>document.write('<div onclick="hide();" style="text-align: center'</P>
<P>+ ';width: ' + (16) + 'px'</P>
<P>+ ';cursor: default' // make mouse arrow stay an arrow instead of turning to 
text arrow</P>
<P>+ ';font-weight: bold'</P>
<P>+ ';background-color: red'</P>
<P>+ ';border: 1px solid ' + text_color</P>
<P>+ ';float: right'</P>
<P>+ ';">'</P>
<P>+ 'X' // write the letter X</P>
<P>+ '</div><br />\n');</P>
<P>document.write('<div id="window_body" style="padding: 5px;">'</P>
<P>+ 'Type in text to find: '</P>
<P>+ '<input type="text" size="25" maxlength="25" id="fwtext"'</P>
<P>+ ' onchange="resettext();">'</P>
<P>+ '<input type="button" value="Find Prev" onclick="findprev();">'</P>
<P>+ '<input id="btn" type="button" value="Find Next" 
onclick="findit();">'</P>
<P>+ '</div>\n'</P>
<P>+ '<div id="test"><br /></div>');</P>
<P>document.write('</div>');</P>
<P>document.write('<input type="button" value="Find on this page..."'</P>
<P>+ ' onclick="show();">');</P>
<P>var findwindow = document.getElementById('findwindow');</P>
<P>var over = 0;</P>
<P>var objDiv = null;</P>
<P>var DivID = null;</P>
<P>var test = document.getElementById('test');</P>
<P> </P>
<P> </P></SPAN>
  Shunmuga Nathan replied to ekta Garg
01-Sep-10 12:58 AM

Sorry for my last post,
Try the following script.

<script type="text/javascript" language="JavaScript"

src="find.js">

</script>

<input type="button" value="Find on this page..."

onclick="show();">

 

var window_background = "white"; // the color of the pop-up window

var window_border = "blue"; // the border color of pop-up window

var text_color = "black"; // the color of the text in window

var title_color = "white"; // color of window title text

var window_width = 200; // width of window

var window_height = 150; // height of window

var mozilla_opt = 1; // change to 0 to use Netscape and Firefox built-in search window

var start_at = 0; // Change to which character you want to start with on the page if IE gives an error because of searching in menus

var ie = (document.all)

if (window.find)

var nav = 1; // to detect if netscape or firefox

else

var nav = 0;

var t = 0; // used for timer to move window in IE when scrolling

var sel; // Selection object needed for Firefox

var range; // range object needed for Firefox

if (!ie)

{

document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);

}

document.onmousedown = MouseDown;

document.onmousemove = MouseMove;

document.onmouseup = MouseUp;

// Temporary variables to hold mouse x-y pos's

var mousex = 0;

var mousey = 0;

if (ie)

{

var txt = document.body.createTextRange();

var bookmark = new Array();

}

var finds = 0;

function findit()

{

var string = document.getElementById('fwtext').value;

findwindow.style.visibility = 'hidden';

if (ie)

{

bookmark[finds] = txt.getBookmark();

if (string) // only call findText if there is a string or IE will have error

if (txt.findText(string)) // if found

{

txt.select();

txt.scrollIntoView();

txt.collapse(false);

test.innerHTML = "Found";

}

else

{

test.innerHTML = "Not found";

finds--;

}

}

else // Netscape or firefox

{

if (finds > 0)

{

sel = window.getSelection(); // get selection

if(sel.rangeCount > 0) sel.removeAllRanges();

sel.addRange(range);

}

if (string != "")

test.innerHTML = window.find(string, false, false);

sel = window.getSelection(); // get selection

range = sel.getRangeAt(0); // get object

}

finds++;

findwindow.style.visibility = 'visible';

 

} // end function findit()

 

// This function is to find backwards by pressing the Prev button

function findprev()

{

var string = document.getElementById('fwtext').value;

findwindow.style.visibility = 'hidden';

if (ie)

{

if (finds < 2)

{

findwindow.style.visibility = 'visible';

return;

}

finds = finds - 2; // I don't know why I have to go back 2

txt.moveToBookmark(bookmark[finds]);

findit();

}

else // if netscape or firefox

{

if (finds > 0)

{

sel = window.getSelection(); // get selection

if(sel.rangeCount > 0) sel.removeAllRanges();

sel.addRange(range);

}

if (string != "")

test.innerHTML = window.find(string, false, true);

sel = window.getSelection(); // get selection

range = sel.getRangeAt(0); // get object

}

 

findwindow.style.visibility = 'visible';

 

} // end findprev()

function checkkey(e)

{

var keycode;

if (window.event) // if ie

keycode = window.event.keyCode;

else // if Firefox or Netscape

keycode = e.which;

if (keycode == 13) // if ENTER key

{

if (ie)

document.getElementById('btn').focus();

findit(); // call findit() function (like pressing NEXT)

}

} // end function checkkey()

function show()

{

if (ie || mozilla_opt == 1)

{

var textbox = document.getElementById('fwtext');

findwindow.style.visibility = 'visible';

textbox.focus();

t = setInterval('move_window();', 500);

document.onkeydown = checkkey;

}

else // if netscape or firefox

window.find();

} // end function show()

function hide()

{

findwindow.style.visibility = 'hidden';

clearTimeout(t);

document.onkeydown = null;

} // end function hide()

function resettext()

{

if (ie)

{

txt = document.body.createTextRange();

txt.moveStart("character", start_at);

}

finds = 0;

} // end function reset()

function move_window()

{

fwtop = parseFloat(findwindow.style.top);

fwleft = parseFloat(findwindow.style.left);

fwheight = parseFloat(findwindow.style.height);

if (document.documentElement.scrollTop) // Needed if you use doctype loose.htm

current_top = document.documentElement.scrollTop;

else

current_top = document.body.scrollTop;

if (document.documentElement.clientHeight)

{

if (document.documentElement.clientHeight > document.body.clientHeight)

current_bottom = document.body.clientHeight + current_top;

else

current_bottom = document.documentElement.clientHeight + current_top;

}

else

current_bottom = document.body.clientHeight + current_top;

if (document.documentElement.scrollLeft) // Needed if you use doctype loose.htm

current_left = document.documentElement.scrollLeft;

else

current_left = document.body.scrollLeft;

if (document.documentElement.clientWidth)

{

if (document.documentElement.clientWidth > document.body.clientWidth)

current_right = document.body.clientWidth + current_left;

else

current_right = document.documentElement.clientWidth + current_left;

}

else

current_right = document.body.clientWidth + current_left;

if (fwtop < current_top)

{

findwindow.style.top = current_top + 'px';

}

else if (fwtop > current_bottom - fwheight)

{

findwindow.style.top = current_bottom - fwheight + 'px';

}

if (fwleft < current_left ||

fwleft > current_right)

{

findwindow.style.left = current_left + 'px';

}

} // end function move_window()

 

function MouseDown(e)

{

if (over == 1)

DivID = 'findwindow';

 

if (over)

{

if (ie)

{

objDiv = document.getElementById(DivID);

objDiv = objDiv.style;

mousex=event.offsetX;

mousey=event.offsetY;

}

else // if Mozilla or Netscape

{

objDiv = document.getElementById(DivID);

mousex=e.layerX;

mousey=e.layerY;

return false;

}

}

}

function MouseMove(e)

{

 

if (document.documentElement.scrollTop) // Needed if you use doctype loose.htm

current_top = document.documentElement.scrollTop;

else

current_top = document.body.scrollTop;

if (document.documentElement.scrollLeft) // Needed if you use doctype loose.htm

current_top = document.documentElement.scrollLeft;

else

current_left = document.body.scrollLeft;

 

 

if (objDiv)

{

if (ie)

{

objDiv.pixelLeft = event.clientX-mousex + current_left;

objDiv.pixelTop = event.clientY-mousey + current_top;

return false;

}

else // if Mozilla or Netscape

{

objDiv.style.left = (e.pageX-mousex) + 'px';

objDiv.style.top = (e.pageY-mousey) + 'px';

return false;

}

}

} // end function MouseMove(e)

function MouseUp()

{

objDiv = null;

}

document.write('<div id="findwindow" style="position:absolute'

+ ';left: 0px; top: 0px'

+ ';visibility: hidden'

+ ';background-color: ' + window_background

+ ';border: 2px solid ' + window_border

+ ';width: ' + window_width + 'px'

+ ';height: ' + window_height + 'px'

+ ';color: ' + text_color

+ ';padding: 0px'

+ ';font-size: 14px'

+ ';"'

+ '>');

document.write('<div style="text-align: center'

+ ';width: ' + (window_width-20) + 'px'

+ ';cursor: move' // turn mouse arrow to move icon

+ ';color: ' + title_color

+ ';border: 1px solid ' + text_color

+ ';background-color: ' + window_border

+ ';float: left'

+ ';" onmouseover="over=1;" onmouseout="over=0;">'

+ 'Find Window</div>');

document.write('<div onclick="hide();" style="text-align: center'

+ ';width: ' + (16) + 'px'

+ ';cursor: default' // make mouse arrow stay an arrow instead of turning to text arrow

+ ';font-weight: bold'

+ ';background-color: red'

+ ';border: 1px solid ' + text_color

+ ';float: right'

+ ';">'

+ 'X' // write the letter X

+ '</div><br />\n');

document.write('<div id="window_body" style="padding: 5px;">'

+ 'Type in text to find: '

+ '<input type="text" size="25" maxlength="25" id="fwtext"'

+ ' onchange="resettext();">'

+ '<input type="button" value="Find Prev" onclick="findprev();">'

+ '<input id="btn" type="button" value="Find Next" onclick="findit();">'

+ '</div>\n'

+ '<div id="test"><br /></div>');

document.write('</div>');

document.write('<input type="button" value="Find on this page..."'

+ ' onclick="show();">');

var findwindow = document.getElementById('findwindow');

var over = 0;

var objDiv = null;

var DivID = null;

var test = document.getElementById('test');

 

 

Create New Account
help