9

As of now I am using the JavaScript search method to replace some text in body HTML like below..

Suppose my html is

 <body>
      <div> I am in body...</div>
 </body>

Then I am using the below approach

 var body = $(body);
 var text = body.html();
 text = text.replace('I am in body...','Yes');
 body.html(text);

But I could see slow page search in browsers like IE..
Please suggest me to improve this and also please let know if there are any plugins to search and match a string even if it contains any html tags in it.. Like below

 <body>
       <div><span>I </span> am in body...</div>
 </body>

With the current method I cannot match this..
If i use

  $('*:contains("some search text string")'); 

This will match the DIV and BODY as well.. But here I want to search the html text not parent element... Thanks

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Exception
  • 8,111
  • 22
  • 85
  • 136

4 Answers4

8

You should take a look at :

ranges:(to modify text without overwriting the complete body)

IE: http://msdn.microsoft.com/en-us/library/ms535872%28v=vs.85%29.aspx
Others: https://developer.mozilla.org/en/DOM/range


find()/findText()(to create the ranges)

IE: http://msdn.microsoft.com/en-us/library/ms536422%28v=vs.85%29.aspx
Others: https://developer.mozilla.org/en/DOM/window.find

(Opera doesn't support find or findText)


Regarding to that question a small modification:

<html>
<head>

  <script>
  function fx(a,b)
  {
    if(window.find)
    {
      while(window.find(a))
      {

        var rng=window.getSelection().getRangeAt(0);
        rng.deleteContents();

        rng.insertNode(document.createTextNode(b));

      }
    }
    else if(document.body.createTextRange)
    {
      var rng=document.body.createTextRange();
      while(rng.findText(a))
      {
        rng.pasteHTML(b);
      }
    }
  }
  </script>
</head>
<body onload="fx('I am in body...','Yes')">
<div>Oh <span>I </span>am in body...<i>, wonderful</i></div>
</body>
</html>
Community
  • 1
  • 1
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
1

you will get only the elements that contain the full text
without being a parent to some other element that contains the same text

var str = "some search text string";
var elements = $("*:contains('"+ str +"')").filter(
                  function(){
                      return $(this).find("*:contains('"+ str +"')").length == 0
                  }
               );
unloco
  • 6,928
  • 2
  • 47
  • 58
1

I think your code is work well, but your error is you didn't figure out the "body" content.

Here is your code :

$(document).ready(function(){
    var body = $("body");    // your miss this
    var text = body.html();
    text = text.replace('I am in body...','Yes');
    body.html(text);
});
Kannika
  • 2,538
  • 2
  • 27
  • 38
0

Try this:

$(function() {
    var foundin = $('body:contains("some search text string")');
});

Hope it helps

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • I can use $('*:contains("some search text string")'); but I just want to search the string not the parent.. Also this will match the parent tag and also body tag.. – Exception Nov 26 '11 at 03:59