Search in page - JavaScript

JavaScript / Ajax etc.

Search in page - JavaScript

Postby cata45 » 06 Nov 2011, 15:56

Am nevoie de un script care sa imi sublinieze (sau sa scoata in evidenta) cuvantul sau fraza cautata. (ceva gen ctrl+F)
Ce am pana acum (am gasit pe net) merge doar in IE si NS. Cum pot sa il fac sa mearga si pe Mozilla si Chrome?

  1.  
  2. <script language="JavaScript">
  3. var NS4 = (document.layers); // Which browser?
  4. var IE4 = (document.all);
  5. var win = window; // window to search.
  6. var n = 0;
  7.  
  8. function findInPage(str) {
  9.     var txt, i, found; if (str == "") return false;
  10.    
  11.     // Find next occurance of the given string on the page, wrap around to the start of the page if necessary.
  12.     if (NS4) {
  13.         // Look for match starting at the current point. If not found, rewind back to the first match.
  14.         if (!win.find(str)) while(win.find(str, false, true)) n++;
  15.         else n++;
  16.         // If not found in either direction, give message.
  17.         if (n == 0) alert("Not found.");
  18.     }
  19.  
  20.     if (IE4) {
  21.         txt = win.document.body.createTextRange();
  22.         // Find the nth match from the top of the page.
  23.         for (i = 0; i <= n && (found = txt.findText(str)) != false; i++) {
  24.             txt.moveStart("character", 1); txt.moveEnd("textedit");
  25.         }
  26.         // If found, mark it and scroll it into view.
  27.         if (found) {
  28.             txt.moveStart("character", -1);
  29.             txt.findText(str);
  30.             txt.select();
  31.             txt.scrollIntoView();
  32.             n++;
  33.         }
  34.         // Otherwise, start over at the top of the page and find first match.
  35.         else {
  36.             if (n >0) {
  37.                 n = 0; findInPage(str);
  38.             }
  39.             // Not found anywhere, give message.
  40.             else alert("Not found.");
  41.         }
  42.     }
  43.     return false;
  44. }
  45. </script>
  46.  
  47. <form name="search">
  48. <font size="3"><input name="string" type="text" size="15" onChange="javascript&#058; numtimes=0;"></font>
  49. <input type="button" value="Search Page" onclick="return findInPage(document.search.string.value);">
  50. </form>


Am cautat foarte mult asa ceva dar ce am gasit nu este functional pe Chrome sau Mozilla (sau ce functioneaza pe Mozilla nu functioneaza pe Chrome si invers :( )
0,0p / 0 votes
The EARTH without ART is just EH.
User avatar
cata45
Byte
 
Joined: 02 Sep 2010
Status: 9

Re: Search in page - JavaScript

Postby smith » 07 Nov 2011, 23:13

Dar de ce ai avea nevoie de așa ceva?
0,0p / 0 votes
Ilea Cristian
User avatar
smith
Enum
 
Joined: 29 Dec 2009
Location: Cluj-Napoca
Status: 82

Re: Search in page - JavaScript

Postby cata45 » 08 Nov 2011, 13:44

La sfarsitul lunii trbuie sa prezint un proiect la economie. Trebuie sa realizez o pagina web in care sa includ anumite lucruri. Si unul din ele este acesta. (Sunt foarte multi termeni legati de economie si este foarte mult text pe o pagina...probabil la asta s-au gandit cei care au propus tema) Si tin sa mearga in chrome si mozilla pentru ca doar in acestea doua il pot prezenta. Nu stiu cat de complicat este algoritmul (dar trebuie sa caute cuvantul doar in pagina de unde este apelata functia cautare...deci nu e ceva sa caute in tot siteul si sa returneze adresele paginilor unde a gasit cuvinte) Nu stiu daca e complicat sau simplu (nu ma pricep la asa ceva )
0,0p / 0 votes
The EARTH without ART is just EH.
User avatar
cata45
Byte
 
Joined: 02 Sep 2010
Status: 9


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests