Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Find on Page script

    I need some help finding a "find on page" script that will display the results in the middle of the page for IE. I have tried several different ones to no avail. The ones that work in all the browsers display the result in IE at the very bottom of the page. I am not very familiar with programing in Java and I am unable to modify any for my needs. The one I found that did work in IE didn't work in any other browsers. Please help, Thx

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,585
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Rather than making us write one from scratch, show us one you like that works in Firefox and we'll adapt it to IE. It should be trivial. Just a few minor changes.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thx OP it might be easier to do this. Here is a script that works in IE, Firefox, Opera, Navigator etc. It works in everything I can tell but Safari and Google Chrome. It might be easier to add a code for Safari and Google than the other way around.

    Here is the code that works in IE, etc.


    Code:
    <script>
    <!-- Hide from old browsers
    
    /******************************************
    * Find In Page Script -- Submitted/revised by Alan Koontz (alankoontz@REMOVETHISyahoo.com)
    * Visit Dynamic Drive (http://www.dynamicdrive.com/) for full source code
    * This notice must stay intact for use
    ******************************************/
    
    // revised by Alan Koontz -- May 2003
    
    var TRange = null;
    var dupeRange = null;
    var TestRange = null;
    var win = null;
    
    // SELECTED BROWSER SNIFFER COMPONENTS DOCUMENTED AT
    // http://www.mozilla.org/docs/web-devel...
    
    var nom = navigator.appName.toLowerCase();
    var agt = navigator.userAgent.toLowerCase();
    var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);
    var is_ie = (agt.indexOf("msie") != -1);
    var is_ie4up = (is_ie && (is_major >= 4));
    var is_not_moz = (agt.indexOf('netscape')!=-1)
    var is_nav = (nom.indexOf('netscape')!=-1);
    var is_nav4 = (is_nav && (is_major == 4));
    var is_mac = (agt.indexOf("mac")!=-1);
    var is_gecko = (agt.indexOf('gecko') != -1);
    var is_opera = (agt.indexOf("opera") != -1);
    
    // GECKO REVISION
    
    var is_rev=0
    if (is_gecko) {
    temp = agt.split("rv:")
    is_rev = parseFloat(temp[1])
    }
    
    // USE THE FOLLOWING VARIABLE TO CONFIGURE FRAMES TO SEARCH
    // (SELF OR CHILD FRAME)
    
    // If you want to search another frame, change from "self" to
    // the name of the target frame:
    // e.g., var frametosearch = 'main'
    
    //var frametosearch = 'main';
    var frametosearch = self;
    
    function search(whichform, whichframe) {
    
    // TEST FOR IE5 FOR MAC (NO DOCUMENTATION)
    
    if (is_ie4up && is_mac) return;
    
    // TEST FOR NAV 6 (NO DOCUMENTATION)
    
    if (is_gecko && (is_rev <1)) return;
    
    // TEST FOR Opera (NO DOCUMENTATION)
    
    if (is_opera) return;
    
    // INITIALIZATIONS FOR FIND-IN-PAGE SEARCHES
    
    if(whichform.findthis.value!=null && whichform.findthis.value!='') {
    
    str = whichform.findthis.value;
    win = whichframe;
    var frameval=false;
    if(win!=self)
    {
    
    frameval=true; // this will enable Nav7 to search child frame
    win = parent.frames[whichframe];
    
    }
    
    }
    
    else return; // i.e., no search string was entered
    
    var strFound;
    
    // NAVIGATOR 4 SPECIFIC CODE
    
    if(is_nav4 && (is_minor < 5)) {
    
    strFound=win.find(str); // case insensitive, forward search by default
    
    // There are 3 arguments available:
    // searchString: type string and it's the item to be searched
    // caseSensitive: boolean -- is search case sensitive?
    // backwards: boolean --should we also search backwards?
    // strFound=win.find(str, false, false) is the explicit
    // version of the above
    // The Mac version of Nav4 has wrapAround, but
    // cannot be specified in JS
    
    }
    
    // NAVIGATOR 7 and Mozilla rev 1+ SPECIFIC CODE (WILL NOT WORK WITH NAVIGATOR 6)
    
    if (is_gecko && (is_rev >= 1)) {
    
    if(frameval!=false) win.focus(); // force search in specified child frame
    strFound=win.find(str, false, false, true, false, frameval, false);
    
    // The following statement enables reversion of focus 
    // back to the search box after each search event 
    // allowing the user to press the ENTER key instead
    // of clicking the search button to continue search.
    // Note: tends to be buggy in Mozilla as of 1.3.1
    // (see www.mozilla.org) so is excluded from users 
    // of that browser.
    
    if (is_not_moz) whichform.findthis.focus();
    
    // There are 7 arguments available:
    // searchString: type string and it's the item to be searched
    // caseSensitive: boolean -- is search case sensitive?
    // backwards: boolean --should we also search backwards?
    // wrapAround: boolean -- should we wrap the search?
    // wholeWord: boolean: should we search only for whole words
    // searchInFrames: boolean -- should we search in frames?
    // showDialog: boolean -- should we show the Find Dialog?
    
    }
    
    if (is_ie4up) {
    
    // EXPLORER-SPECIFIC CODE revised 5/21/03
    
    if (TRange!=null) {
    
    TestRange=win.document.body.createTextRange();
    
    if (dupeRange.inRange(TestRange)) {
    
    TRange.collapse(false);
    strFound=TRange.findText(str);
    if (strFound) {
    //the following line added by Mike and Susan Keenan, 7 June 2003
    win.document.body.scrollTop = win.document.body.scrollTop + TRange.offsetTop;
    TRange.select();
    }
    
    }
    
    else {
    
    TRange=win.document.body.createTextRange();
    TRange.collapse(false);
    strFound=TRange.findText(str);
    if (strFound) {
    //the following line added by Mike and Susan Keenan, 7 June 2003
    win.document.body.scrollTop = TRange.offsetTop;
    TRange.select();
    }
    
    }
    }
    
    if (TRange==null || strFound==0) {
    TRange=win.document.body.createTextRange();
    dupeRange = TRange.duplicate();
    strFound=TRange.findText(str);
    if (strFound) {
    //the following line added by Mike and Susan Keenan, 7 June 2003
    win.document.body.scrollTop = TRange.offsetTop;
    TRange.select();
    }
    
    }
    
    }
    
    if (!strFound) alert ("String '"+str+"' not found!") // string not found
    
    }
    // -->
    </script>
    
    <!-- EXAMPLE FORM OF FIND-IN-PAGE SEARCH USING SUBMIT (ALLOWING 'ENTER/RETURN' KEY PRESS EVENT) -->
    <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>
    
    
    Here is the code that works in Safari and Google chrome.
    
    <form name="f1" action="" 
    onSubmit="if(this.t1.value!=null && this.t1.value!='')
    findString(this.t1.value);return false"
    >
    <input type="submit" style="background-color:#A0794D;font-family:Ariel,sans-serif;color:white;font-weight:bold;height: 22px; width: 80px;border:1px solid;border-color:white" name=b1 value="Property #">
    <input type="text" name=t1 value="" style="background-color:E5F3F3;font-weight:bold;height: 22px; width: 50px;border:1px solid;border-color:white">
    </form>
    
    <script language="JavaScript">
    
    var TRange=null
    
    function findString (str) {
     if (parseInt(navigator.appVersion)<4) return;
     var strFound;
     if (window.find) {
    
        strFound=self.find(str);
      if (!strFound) {
       strFound=self.find(str,0,1)
       while (self.find(str,0,1)) continue
      }
     }
     else if (navigator.appName.indexOf("Microsoft")!=-1) {
    
      // EXPLORER-SPECIFIC CODE
    
      if (TRange!=null) {
       TRange.collapse(false)
       strFound=TRange.findText(str)
       if (strFound) TRange.select()
      }
      if (TRange==null || strFound==0) {
       TRange=self.document.body.createTextRange()
       strFound=TRange.findText(str)
       if (strFound) TRange.select()
      }
     }
      else if (navigator.appName=="Opera") {
      alert ("Opera browsers not supported, sorry...")
      return;
     }
     if (!strFound) alert ("Property # '"+str+"' is not found or under contract!")
     return;
    }
    
    
    </script>


    I am not sure which would be easier to modify so that is why I sent both. Thanks Jess
    Last edited by jlc680; 08-22-2010 at 05:09 PM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    You should note that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

    This is exceedingly old and obsolete code, IE4 and NS4 date from 1997.
    <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead.


    You do not make it clear what you mean by a "find on page script", but this will find multiple words/phrases in the <body> of the page (obviously those words/phrases can be variables rather than hard coded for the purposes of the example):

    Code:
    <html>
    <head>
    
    <script type="text/javascript" >
    
    var keywords = [];
    keywords[0] = "consectetur";
    keywords[1] = "Sed euismod felis";
    keywords[2] = "Duis laoreet elit";
    keywords[3] = "felis";
    var len = keywords.length;
    var found = [];
    for (var i =0; i < len; i++) {
    found[i] = 0;
    }
    
    </script>
    
    </head>
    
    <body onload = "findStuff()">
    
    <p>Lorem ipsum dolor <b>sit amet,</b> consectetur <i>adipiscing</i> elit. </p>
    <p>Cras suscipit ullamcorper elit vitae <br>sodales. Sed euismod felis molestie</p>
    
    <script type = "text/javascript">
    
    function findStuff() {
    var str = document.getElementsByTagName('body')[0].innerHTML;
    // strip all HTML tags out of that:
    var zapRE = /\<[a-z][^\>]*\>/ig;
    str = str.replace(zapRE, "");
    
    //str = str.toLowerCase();  // to make case insensitive
    for (var i = 0; i<len; i++) {
    str2find = keywords[i];
    //str2find=str2find.toLowerCase();  // to make case insensitive
    var a = str.indexOf(str2find);
    if (a >= 0) {
    found [i] = 1;
    }
    }
    
    for (var i = 0; i < len; i++) {
    if (found[i] == 1) {
    alert ("The BODY contains the string:- " + keywords[i]);
    }
    }
    
    for (var i = 0; i < len; i++) {
    if (found[i] == 0) {
    alert ("The BODY does NOT contain the string:- " + keywords[i]);
    }
    }
    
    }
    
    </script>
    
    </body>
    </html>
    We could probably give you better advice if you specified more clearly what you are aiming for. Obviously the position where the result is displayed on the page is a matter of HTML.

    BTW, please help us to help you by following the posting guidelines and wrapping your code in [ code ][ /code ] tags (omit the spaces). This means use the octothorpe or # button on the toolbar which will insert the tags. You can (and should) edit your previous post.



    Quizmaster: How many countries in the world have names which, in English, begin with the letter Q?
    Contestant: I can only think of one - Quba.
    Last edited by Philip M; 08-22-2010 at 10:04 AM. Reason: Noticed typo

  • #5
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Philip but that is not really what I am looking for it to perform. Let me start by apologizing if my terminology is incorrect. I am a novice who is attempting to teach myself. I am just seeking knowledge from those who hold what I do not have at this point. I thank anyone 10x over who is able to help me out. I will clarify what I need the script to perform. On my website I am planning to have one page that has several ads with individual ad numbers. I would like the user to be able to input the ad number into a search field and then have the script search the page for the number. Once the number is found I would like it to be highlighted and displayed in the middle of the screen. One of the scripts listed in my previous post worked in most browsers but would show the results in IE at the very bottom of the page (which could be easily over looked). The other script I posted in the previous post displayed the results in the middle of the page for IE but did not work at all in Google Chrome or Safari. If what I am requesting is totally off base or impossible to understand please let me know. Thanks Jess
    Last edited by jlc680; 08-22-2010 at 08:32 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •