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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to search in text with javascript

    Found this link
    http://www.javascripter.net/faq/searchin.htm
    i have used a part of the code

    PHP Code:
    <form onSubmit="if(this.searchField.value != null && this.searchField.value != '')searchText(this.searchField.value);return false">
        <
    input type="text" name="searchField" size="20">
        <
    input type="submit"  value="Search">

    </
    form

    PHP Code:
    function searchText(str)
    {
    var 
    TRange=null;


        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()
          }

         if (!
    strFound
         
    alert ("String '"+str+"' not found!")    

    The problem is that the javascript is located in base.js och the searchform is in mGlossary.htm and the text to search is in glossary.htm. How could i search the text in glossary.htm

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the JS needs to be every HTML file
    *keep it simple (TM)

  • #3
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Couldn´t it be done when i have the text field and search button in mGlossary.htm and the text to search in glossary.htm

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Javascript has to be on every page to work. it can't go and look at other pages. period.
    *keep it simple (TM)

  • #5
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could I inwoke the search function from another side for example mGlossary.htm? If i have the search function in glossary.htm

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you're just not getting this, are you. outside of framesets, if you have a piece of Javacsript in one page, another HTML page cannot get access to it. and to repeat what i said about, Javascript cannot access pages which didn't call them.

    kapiche?
    *keep it simple (TM)

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by jbot
    outside of framesets
    You have to include iframes and 2 opened windows (of the same domain) where one opened the other. They can access each other's script.

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    You have to include iframes and 2 opened windows (of the same domain) where one opened the other. They can access each other's script.
    i understand that ... i just subsume those under framesets for convience given they somewhat work in the same way.
    *keep it simple (TM)

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Quote Originally Posted by jbot
    i understand that ... i just subsume those under framesets for convience given they somewhat work in the same way.
    It's better to make it specific than to generalize it like that to avoid confusion to the poster. He/she might get the impression that you can only access scripts within real framesets.

    Going back to the question...
    If you want to search a certain page and the search field and button is on another page, you have to use frames, iframes or open the other page in another window. In other words, the 2 pages must be loaded at the same time to make them work together.

  • #10
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have an two iframes on one page called index_tabs.htm one iframe with id="menu" in this i load the file mGlossary.htm (menu), this one contains the search text and the button submit.
    In the other iframe with id="show" i load a file with name glossary.htm in this one i have the text to search

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts
    Modify the script like this:
    Code:
    <script language="JavaScript">
    <!--
    var TRange=null
    
    function findString (str, win) {
     if (parseInt(navigator.appVersion)<4) return;
     var strFound;
     if (navigator.appName=="Netscape") {
    
     if (!win) win = self;
     win.focus();
    
      // NAVIGATOR-SPECIFIC CODE
    
      strFound=win.find(str);
      if (!strFound) {
       strFound=win.find(str,0,1)
       while (win.find(str,0,1)) continue
      }
     }
     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=win.document.body.createTextRange()
       strFound=TRange.findText(str)
       if (strFound) TRange.select()
      }
     }
     if (!strFound) alert ("String '"+str+"' not found!")
    }
    
    //-->
    </script>
    ...
    <iframe id="show" src="glossary.htm"></iframe>
    <form name="f1" action="" 
    onSubmit="if(this.t1.value!=null && this.t1.value!='')
    findString(this.t1.value, document.getElementById('show').contentWindow);return false"
    >
    <input type="text" name=t1 value="text" size=20>
    <input type="submit" name=b1 value="Find">
    </form>

  • #12
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think you missunderstod me a little bit

    Hera is the code without javascript

    Code for index_tabs.htm

    PHP Code:
    <html>
    <
    head>
    <
    title></title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    link rel="stylesheet" href="CSS/base.css" type="text/css">
    <
    script type="text/javascript" language="javascript" src="Javascript/base.js"></script>

    </head>

    <body onLoad="javascript:init()">

    <div id="globalcontainer">
        <div id="header">
            <ul>
                <li id="current"><a href="#" onClick="activateTab(this.parentNode, 'home')">Home</a></li>
                <li><a href="#" onClick="activateTab(this.parentNode, 'channels')">Channels</a></li>
                <li><a href="#" onClick="activateTab(this.parentNode, 'diagnosis')">Diagnosis and treatment</a></li>
                <li><a href="#" onClick="activateTab(this.parentNode, 'glossary')">Glossary</a></li>
                <li><a href="#" onClick="activateTab(this.parentNode, 'needling')">Needling</a></li>
                <li><a href="#" onClick="activateTab(this.parentNode, 'pointCat')">Point categorie</a></li>
                <li><a href="#" onClick="activateTab(this.parentNode, 'pointindex')">Point index</a></li>
                <!--<li><a href="#" onClick="activateTab(this.parentNode, 'methods')">Point selection methods</a></li>-->
            </ul>
        </div>
        <br style="clear:left"/>
        <span class="style1"></span>
        <div id="box">
                <iframe id="meny" width="595" height="100" src="Menu/mHome.htm" frameborder="0"></iframe>
        
             <!-- Detta har jag gjort för att vi skall uppdatera bara meny diven.
            <form name="head">
                <select name="headselect" onChange="populate(this.selectedIndex, this.form)">
                    <option>Välj meridian</option>
                    <option>Meridian 1</option>
                    <option>Meridian 2</option>
                    <option>Meridian 3</option>
                 </select><br><br>
         
                 <select name="subselect" onChange="change(this.options[selectedIndex].value)">
                    <option>--</option>     
              </select>
              </form>
            -->
      </div >
      
        
        
        <div id="view">
            <iframe name="show" id="show" src="Javascript/base.js" width="595" height="445"></iframe>
      </div> 
    </div>
        
    </body>
    </html> 
    code for mGlossary.htm

    PHP Code:

    <head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    link rel="stylesheet" href="../CSS/menu.css" type="text/css">
    <
    script language="javascript" src="../Data/Presentation/glossary.htm"></script>
    </head>

    <body>
    <a href="../Data/Presentation/glossary.htm#A" target="show">A</a> | <a href="../Data/Presentation/glossary.htm#B" target="show">B</a> | <a href="../Data/Presentation/glossary.htm#C" target="show">C</a> | <a href="../Data/Presentation/glossary.htm#D target="show"">D</a> | <a href="../Data/Presentation/glossary.htm#E" target="show">E</a> | <a href="../Data/Presentation/glossary.htm#F" target="show">F</a> | <a href="../Data/Presentation/glossary.htm#G" target="show">G</a> | 
    <a href="../Data/Presentation/glossary.htm#H" target="show">H</a> | <a href="../Data/Presentation/glossary.htm#I" target="show">I</a> | <a href="../Data/Presentation/glossary.htm#J" target="show">J</a> | <a href="../Data/Presentation/glossary.htm#K" target="show">K</a> | <a href="../Data/Presentation/glossary.htm#L" target="show">L</a> | <a href="../Data/Presentation/glossary.htm#M" target="show">M</a> | <a href="../Data/Presentation/glossary.htm#N" target="show">N</a> |
    <a href="../Data/Presentation/glossary.htm#O" target="show">O</a> | <a href="../Data/Presentation/glossary.htm#P" target="show">P</a> | <a href="../Data/Presentation/glossary.htm#Q" target="show">Q</a> | <a href="../Data/Presentation/glossary.htm#R" target="show">R</a> | <a href="../Data/Presentation/glossary.htm#S" target="show">S</a> | <a href="../Data/Presentation/glossary.htm#T" target="show">T</a> | <a href="../Data/Presentation/glossary.htm#U" target="show">U</a> |
    <a href="../Data/Presentation/glossary.htm#V" target="show">V</a> | <a href="../Data/Presentation/glossary.htm#W" target="show">W</a> | <a href="../Data/Presentation/glossary.htm#X" target="show">X</a> | <a href="../Data/Presentation/glossary.htm#Y" target="show">Y</a> | <a href="../Data/Presentation/glossary.htm#Z" target="show">Z</a> |<br>

    <!--if(this.t1.value!=null && this.t1.value!='')findString(this.t1.value);return false"-->
    <form onSubmit="if(this.searchField.value != null && this.searchField.value != '')searchText(this.searchField.value);return false">
        <input type="text" name="searchField" size="20">
        <input type="submit"  value="Search">

    </form>
    </body>
    </html> 
    code for glossary.htm

    PHP Code:
    <html>
    <
    head>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    link rel="stylesheet" href="../../CSS/base.css" type="text/css">


    <
    title>Glossary</title>
    </
    head>

    <
    body>

    <!--
    <
    form name="search" onSubmit="return findInPage(this.string.value);"
    <
    font size=3
    <
    input type="submit" value="Sök efter"
    <
    input name="string" type="text" size="20" onChange="n = 0;"
    </
    font
    </
    form
    -->


    <
    div id="search">
        <
    form name="searchField" onSubmit="findInPage(this.string.value);">
        <
    font style="font-size:10px ">
        <
    input style="font-size:9px " type="text" size="20">
        <
    input style="font-size:9px " type="submit" value="Search">
        </
    font>
        
        
        </
    form>


    </
    div>

    <
    div id="mass">
    <
    a name="A">A</a><br>
    <
    a name="B">B</a><br>
    <
    a name="C">C</a><br>
    <
    a name="D">D</a><br>
    <
    a name="E">E</a><br>
    <
    a name="F">F</a><br>
    <
    a name="G">G</a><br>
    <
    a name="H">H</a><br>
    <
    a name="I">I</a><br>
    <
    a name="J">J</a><br>
    <
    a name="K">K</a><br>
    <
    a name="L">L</a><br>
    <
    a name="M">M</a><br>
    <
    a name="N">N</a><br>
    <
    a name="O">O</a><br>
    <
    a name="P">P</a><br>
    <
    a name="Q">Q</a><br>
    <
    a name="R">R</a><br>
    <
    a name="S">S</a><br>
    <
    a name="T">T</a><br>
    <
    a name="U">U</a><br>
    <
    a name="V">V</a><br>
    <
    a name="W">W</a><br>
    <
    a name="X">X</a><br>
    <
    a name="Y">Y</a><br>
    <
    a name="Z">Z</a>

    </
    div
    Last edited by janne67; 01-20-2005 at 02:52 PM.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,032
    Thanks
    0
    Thanked 248 Times in 245 Posts

    HTML and JavaScript are 2 different things...

    You cannot include an html page as javascript!

    <script language="javascript" src="../Data/Presentation/glossary.htm"></script>

  • #14
    New Coder
    Join Date
    Nov 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry i forgot to remove it. It was a test. I have changed it back now

  • #15
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Search in frames

    I find this code to be very useful, however if you have an html file with a lot of text on it and place the search part at the top of the page and it finds a word lower in the page which causes the page to scroll down then the input and search button will not be visible on the page.

    The user will have to scroll up each time to change their search or to click the search button to find the next time their search text appears on the page. Do you know how to manipulate the code to work in frames so that the search part in on one page and and searches on another page? Like as follows:

    <html>
    <head>
    <title></title>

    <frameset rows="80,*">
    <frame src="file_with_input_and_search_button.html">

    <frameset cols="*">
    <frame name="main" src="file_to_be_searched.html">
    </frameset>
    </frameset>

    </body>
    </html>

    Any help would be greatly appreciated!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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