views:

1081

answers:

2

in a html page, i have

<html>
   <script>
       var cnt=''+document.documentElement.innerHTML+'';
cnt=cnt.replace(......);
   </script>
   <body> something else</body>
</html>

how to use replace function above, so that my 'cnt' var content is like below

<html>
  <body> something else</body>
</html>
A: 
window.onload = function(){
   var body = String(document.body.innerHTML).replace("...");
   document.body.innerHTML = body;

}
andres descalzo
What's the point in wrapping a string in String()?
J-P
none in particular, only one practice for if the item is null. thanks for your wisdom
andres descalzo
+1  A: 

String.replace() is not powerful enough for your case. Use this function:

function stripTags(var s) {
    var start = '<' + s + '>';
    var end = '</' + s + '>';

    while (true) {
        var pos1 = s.indexOf(start);
        var pos2 = s.indexOf(end);
        if (pos1 == -1 || pos2 == -1) { break; }
        s = s.substring(0,pos1) + s.substring(pos2+end.length);
    }
    return s;
}

This allows to remove all text in a specific element (script in your case).

Aaron Digulla