0

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>

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

-1
window.onload = function(){
   var body = String(document.body.innerHTML).replace("...");
   document.body.innerHTML = body;

}

1 Comment

none in particular, only one practice for if the item is null. thanks for your wisdom

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.