Remove everything between tags using javascript

Hi,

I am not good in javascript but i want to remove everything between tag.

Here is an example:

<html>
<head>
</head>
<body>
<p>hello</p>
<b>Hi</b>
<!-- abcxyz —>
<center><a href=“abc.php”>click here</a></center>
<!-- abcxyz —>
</body>
</html>

Here is what I want to remove using javascript.

<!-- abcxyz —>
<center><a href=“abc.php”>click here</a></center>
<!-- abcxyz —>

i want to remove these three line automatically from my code using javascript, I tried lot but unable to remove these tags. Can anyone help me?

Thanks,
Jonathan

You can search the dom for elements with a node name of “#comment” or a node type equal to 8, which will find the comments for you.

Then you can check the data property for " abcxyz -" or use a regular expression if you won’t know the exact formatting of the comment.

The script would be a bit involved. What have you got so far?

I did a proof of concept for the brief example, then realised that it’s been done before. There are some good examples on the web
http://javascript.internet.com/snippets/remove-html-tags.html

But the more common way is to have server-side scripts do the job instead.

var next= document.getElementsByTagName('b');
next= next[next.length-1]
var pa= next.parentNode;
while(next.nextSibling){
	next=next.nextSibling;
	pa.removeChild(next)
}

If you had content you wanted to keep beyond the deleted part,
you would look for it in the loop, and quit when the next node is a ‘keeper’.

Since you want to remove everything from the last b element (b element?) to the end, it is simpler.

I tried these examples of code but my issue is still alive.
Here is my code:


<html>
<head>
</head>
<body>
<h1>Hello</h1>
<b>Hi</b>
<!-- Abc xyz -->
<center><a style="color:#000000; font-size:12px" href="http://mydomain.com" target="_blank">Click here</a></center>
<!-- Abc xyz -->
</body>
</html>

I want to remove these lines from my code using javascript:


<!-- Abc xyz -->
<center><a style="color:#000000; font-size:12px" href="http://mydomain.com" target="_blank">Click here</a></center>
<!-- Abc xyz -->

The reset code should looks like after removing these lines:


<html>
<head>
</head>
<body>
<h1>Hello</h1>
<b>Hi</b>
</body>
</html>

Thank you,
Jonathan

You can’t remove anything from the source code using javascript.
You can only remove elements from the current rendered html of the page.
You need to save the file on the server after the edits, with a server script, to change the source.

:slight_smile:
Ok, I will let you know it is possible but difficult.

it is possible but difficult

If you are talking about changing the document source code, so that if I hit view source I don’t see the lines between your comment tags - it’s impossible using only javascript. As others have said you need to change the code server side so that only the part you want displayed gets sent to the client.

If you are talking about changing the rendered HTML so that the part between your comment tags is not displayed in a browser (but still visible in view source) then yes it’s possible.


document.body.innerHTML = document.body.innerHTML.replace(/<!-- Abc xyz -->[\\s\\S]*<!-- Abc xyz -->/ig, "");

If you are talking about changing the rendered HTML so that the part between your comment tags is not displayed in a browser (but still visible in view source) then yes it’s possible.

Yes, But this code is not working:

document.body.innerHTML = document.body.innerHTML.replace(/<!-- Abc xyz –>[\s\S]*<!-- Abc xyz –>/ig, “”);

Thanks for solving my problem

I recieved the out:

Hello
Hi /ig,""); 

But i need result :

Hello
Hi

Thanks

Which browser did you test it in?

Show your test code please.

Maybe without the “innerHTML”? eg.

document.body = document.body.replace(/<!-- Abc xyz -->[\\s\\S]*<!-- Abc xyz -->/ig, "");

I test the code using IE7 and Firfox 2.0
My issue is resolved because i did it through sever using php script.

Thanks again for help.

Using PHP to adjust the code before it’s handed to the browser is a much better way. Thank you for coming here though, and it’s great to see that you did find a successful solution.