SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,185
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    comments and javascript

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

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    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/snipp...html-tags.html
    http://www.faqts.com/knowledge_base/...html/aid/21761

    But the more common way is to have server-side scripts do the job instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    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.

  6. #6
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried these examples of code but my issue is still alive.
    Here is my code:

    HTML 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:

    HTML Code:
    <!-- 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 Code:
    <html>
    <head>
    </head>
    <body>
    <h1>Hello</h1>
    <b>Hi</b>
    </body>
    </html>
    Thank you,
    Jonathan

  7. #7
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  8. #8
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

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

  10. #10
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  11. #11
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recieved the out:

    HTML Code:
    Hello
    Hi /ig,"");
    But i need result :

    Hello
    Hi

    Thanks

  12. #12
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Which browser did you test it in?

    Show your test code please.

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,185
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    stripping out between comments

    Maybe without the "innerHTML"? eg.
    HTML Code:
    document.body = document.body.replace(/<!-- Abc xyz -->[\s\S]*<!-- Abc xyz -->/ig, "");

  14. #14
    SitePoint Member nacker4it's Avatar
    Join Date
    Mar 2008
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  15. #15
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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