www.webdeveloper.com
Results 1 to 4 of 4

Thread: search on page

  1. #1
    Join Date
    Feb 2006
    Posts
    86

    search on page

    Code:
    <div id='page'>
    asdf
    asdf
    asdffds
    sdfewer
    </div>
     
    <script>
    var search_string=asd;
    if(search_string){
       
    function x(search_string){var r,i,search_string;
    r=page.innerText.createTextRange();
    for(i=0;r.findText(search_string);i++){r.execCommand('BackColor','','006600');r.collapse(false)};
    }
    x(search_string);
    }
    Object doesn't support property or method.
    I need it to work on all browsers..
    Please help.

  2. #2
    Join Date
    Feb 2006
    Posts
    86
    ?..

  3. #3
    Join Date
    Feb 2006
    Posts
    86
    help..

  4. #4
    Join Date
    Jul 2006
    Posts
    45
    Well, I need more info about the goal of what you are doing.
    For now your x function for IE will look like:
    Code:
    function x(search_string){
    var r,i;
    r=document.body.createTextRange();
    for(i=0;r.findText(search_string);i++){
        r.execCommand('BackColor','','006600');r.collapse(false)};
    }
    For Mozilla:
    Code:
    function x(str)
    {
       var elm = document.getElementById('page');
       var text = elm.textContent;  
       var pos =0;
       var prevPos = 0;
       var tEl = elm.firstChild;
       while((pos = text.indexOf(str,pos))>=0)	
       {
             var range = document.createRange();
             range.setStart(tEl,pos - prevPos);
             range.setEnd(tEl,pos + str.length - prevPos);  
             var el = document.createElement("span");	
             el.style.backgroundColor = 'red';		 
             range.surroundContents(el);
             tEl = el.nextSibling;
             prevPos = pos + str.length;
             pos = pos + str.length + 1;
       }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles