By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,611 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,611 IT Pros & Developers. It's quick & easy.

find in page script

P: 5
Is any ColdFusion script I can put on my webpage that will create a search bar so people can type keywords to match it on the current page in my website? It will be exactly like the finder search bar that comes with your browser window, IE; click edit, then find on this page, which brings up the search box.

I know that JavaScript can do this. I tried to insert JavaScript to my .cfm page. It doesn't work. Any help will be appreciated.

Here is the JavaScript and Form code I used:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. <!-- Hide from old browsers
  3.  
  4. /******************************************
  5. * Find In Page Script -- Submitted/revised by Alan Koontz (alankoontz@REMOVETHISyahoo.com)
  6. * Visit Dynamic Drive (http://www.dynamicdrive.com/) for full source code
  7. * This notice must stay intact for use
  8. ******************************************/
  9.  
  10. //  revised by Alan Koontz -- May 2003
  11.  
  12. var TRange = null;
  13. var dupeRange = null;
  14. var TestRange = null;
  15. var win = null;
  16.  
  17.  
  18. //  SELECTED BROWSER SNIFFER COMPONENTS DOCUMENTED AT
  19. //  http://www.mozilla.org/docs/web-deve...wser_type.html
  20.  
  21. var nom = navigator.appName.toLowerCase();
  22. var agt = navigator.userAgent.toLowerCase();
  23. var is_major   = parseInt(navigator.appVersion);
  24. var is_minor   = parseFloat(navigator.appVersion);
  25. var is_ie      = (agt.indexOf("msie") != -1);
  26. var is_ie4up   = (is_ie && (is_major >= 4));
  27. var is_not_moz = (agt.indexOf('netscape')!=-1)
  28. var is_nav     = (nom.indexOf('netscape')!=-1);
  29. var is_nav4    = (is_nav && (is_major == 4));
  30. var is_mac     = (agt.indexOf("mac")!=-1);
  31. var is_gecko   = (agt.indexOf('gecko') != -1);
  32. var is_opera   = (agt.indexOf("opera") != -1);
  33.  
  34.  
  35. //  GECKO REVISION
  36.  
  37. var is_rev=0
  38. if (is_gecko) {
  39. temp = agt.split("rv:")
  40. is_rev = parseFloat(temp[1])
  41. }
  42.  
  43.  
  44. //  USE THE FOLLOWING VARIABLE TO CONFIGURE FRAMES TO SEARCH
  45. //  (SELF OR CHILD FRAME)
  46.  
  47. //  If you want to search another frame, change from "self" to
  48. //  the name of the target frame:
  49. //  e.g., var frametosearch = 'main'
  50.  
  51. //var frametosearch = 'main';
  52. var frametosearch = self;
  53.  
  54.  
  55. function search(whichform, whichframe) {
  56.  
  57. //  TEST FOR IE5 FOR MAC (NO DOCUMENTATION)
  58.  
  59. if (is_ie4up && is_mac) return;
  60.  
  61. //  TEST FOR NAV 6 (NO DOCUMENTATION)
  62.  
  63. if (is_gecko && (is_rev <1)) return;
  64.  
  65. //  TEST FOR Opera (NO DOCUMENTATION)
  66.  
  67. if (is_opera) return;
  68.  
  69. //  INITIALIZATIONS FOR FIND-IN-PAGE SEARCHES
  70.  
  71. if(whichform.findthis.value!=null && whichform.findthis.value!='') {
  72.  
  73.        str = whichform.findthis.value;
  74.        win = whichframe;
  75.        var frameval=false;
  76.        if(win!=self)
  77. {
  78.  
  79.        frameval=true;  // this will enable Nav7 to search child frame
  80.        win = parent.frames[whichframe];
  81.  
  82. }
  83.  
  84.  
  85. }
  86.  
  87. else return;  //  i.e., no search string was entered
  88.  
  89. var strFound;
  90.  
  91. //  NAVIGATOR 4 SPECIFIC CODE
  92.  
  93. if(is_nav4 && (is_minor < 5)) {
  94.  
  95.   strFound=win.find(str); // case insensitive, forward search by default
  96.  
  97. //  There are 3 arguments available:
  98. //  searchString: type string and it's the item to be searched
  99. //  caseSensitive: boolean -- is search case sensitive?
  100. //  backwards: boolean --should we also search backwards?
  101. //  strFound=win.find(str, false, false) is the explicit
  102. //  version of the above
  103. //  The Mac version of Nav4 has wrapAround, but
  104. //  cannot be specified in JS
  105.  
  106.  
  107.         }
  108.  
  109. //  NAVIGATOR 7 and Mozilla rev 1+ SPECIFIC CODE (WILL NOT WORK WITH NAVIGATOR 6)
  110.  
  111. if (is_gecko && (is_rev >= 1)) {
  112.  
  113.     if(frameval!=false) win.focus(); // force search in specified child frame
  114.     strFound=win.find(str, false, false, true, false, frameval, false);
  115.  
  116. //  The following statement enables reversion of focus 
  117. //  back to the search box after each search event 
  118. //  allowing the user to press the ENTER key instead
  119. //  of clicking the search button to continue search.
  120. //  Note: tends to be buggy in Mozilla as of 1.3.1
  121. //  (see www.mozilla.org) so is excluded from users 
  122. //  of that browser.
  123.  
  124.     if (is_not_moz)  whichform.findthis.focus();
  125.  
  126. //  There are 7 arguments available:
  127. //  searchString: type string and it's the item to be searched
  128. //  caseSensitive: boolean -- is search case sensitive?
  129. //  backwards: boolean --should we also search backwards?
  130. //  wrapAround: boolean -- should we wrap the search?
  131. //  wholeWord: boolean: should we search only for whole words
  132. //  searchInFrames: boolean -- should we search in frames?
  133. //  showDialog: boolean -- should we show the Find Dialog?
  134.  
  135.  
  136. }
  137.  
  138.  if (is_ie4up) {
  139.  
  140.   // EXPLORER-SPECIFIC CODE revised 5/21/03
  141.  
  142.   if (TRange!=null) {
  143.  
  144.    TestRange=win.document.body.createTextRange();
  145.  
  146.  
  147.  
  148.    if (dupeRange.inRange(TestRange)) {
  149.  
  150.    TRange.collapse(false);
  151.    strFound=TRange.findText(str);
  152.     if (strFound) {
  153.         //the following line added by Mike and Susan Keenan, 7 June 2003
  154.         win.document.body.scrollTop = win.document.body.scrollTop + TRange.offsetTop;
  155.         TRange.select();
  156.         }
  157.  
  158.  
  159.    }
  160.  
  161.    else {
  162.  
  163.      TRange=win.document.body.createTextRange();
  164.      TRange.collapse(false);
  165.      strFound=TRange.findText(str);
  166.      if (strFound) {
  167.         //the following line added by Mike and Susan Keenan, 7 June 2003
  168.         win.document.body.scrollTop = TRange.offsetTop;
  169.         TRange.select();
  170.         }
  171.  
  172.  
  173.  
  174.    }
  175.   }
  176.  
  177.    if (TRange==null || strFound==0) {
  178.    TRange=win.document.body.createTextRange();
  179.    dupeRange = TRange.duplicate();
  180.    strFound=TRange.findText(str);
  181.     if (strFound) {
  182.         //the following line added by Mike and Susan Keenan, 7 June 2003
  183.         win.document.body.scrollTop = TRange.offsetTop;
  184.         TRange.select();
  185.         }
  186.  
  187.  
  188.    }
  189.  
  190.  }
  191.  
  192.   if (!strFound) alert ("String '"+str+"' not found!") // string not found
  193.  
  194.  
  195. }
  196. // -->
  197. </script>
  198.  
[HTML]<form name="form1" onSubmit="search(document.form1, frametosearch); return false"><input type="text" name="findthis" size="15" title="Press 'ALT s' after clicking submit to repeatedly search page"> <input type="submit" value="Find in Page" ACCESSKEY="s"></form>
[/HTML]
Thanks,

YaoBao
Feb 8 '08 #1

✓ answered by YaoBao

It works now. I pasted the code to wrong place. Thank you very much tacking time to help me. Thank you again. YaoBao

Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
JavaScript is best suited to this task. Does this work on a normal HTML page, i.e. not a Coldfusion file?
Feb 8 '08 #2

P: 5
JavaScript is best suited to this task. Does this work on a normal HTML page, i.e. not a Coldfusion file?

It is work on normal HTML page. But dosn't work with my Coldfusion file. I jsut insert the JavaScript and the FORM above into my coldfusin file. Maybe I missed some code?

Thanks,

YaoBao
Feb 8 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Show the full page code. You can snip the JavaScript part.
Feb 8 '08 #4

P: 5
It works now. I pasted the code to wrong place. Thank you very much tacking time to help me. Thank you again. YaoBao
Feb 8 '08 #5

acoder
Expert Mod 15k+
P: 16,027
No problem, glad you managed to fix it. Post again if you have any more questions.
Feb 9 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.