www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 05-20-2007, 08:05 PM
    robindean robindean is offline
    Registered User
     
    Join Date: Dec 2005
    Posts: 456
    HTML Code Killer

    I'm hoping to devise a javascript that will remove an html comment. "Why" is not so much a concern as "how".

    Another more experienced programmer presented some very interesting code that I'd like to build from.

    The html comment I'm trying to remove is in the <head> of my page and reads:

    Code:
    <!-- </textarea> -->
    In accordance with how toicontien's code appears to function, I've placed comment tags around the other comment, which now has it reading:

    Code:
    <!-- kill --><!-- </textarea> --><!-- kill -->
    The function is as follows:

    Code:
    function KillCode() {
    if (document.getElementsByTagName) {
    var head = document.getElementsByTagName('head')[0];
    var headHTML = head.innerHTML;
    var headHTMLPieces = headHTML.split('<!-- kill -->');
    head.innerHTML = headHTMLPieces[0] + headHTMLPieces[2];
    }
    }
    It's not working yet and I'm not sure why. Can I get some guidance? When/where should I call the function? Is there a better way to remove the html comment using javascript?

    Last edited by robindean; 05-20-2007 at 08:10 PM.
    Reply With Quote
      #2  
    Old 05-21-2007, 01:08 AM
    Jeff Mott's Avatar
    Jeff Mott Jeff Mott is offline
    Super Moderator
     
    Join Date: Jul 2003
    Location: The City of Roses
    Posts: 2,041
    I can somewhat answer your question only because I know your past situation. If you use this code, for instance (I'm using base instead of META refresh only because it's more convenient while testing):
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    
    <html lang="en-us">
    
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
    
    <script type="text/javascript">document.write('<!-- <textarea rows="0" cols="0" style="width: 0px; height: 0px; display: none;">');</script>
    <base href="http://www.w3.org/">
    <!-- kill --><!-- </textarea> --><!-- kill -->
    </head>
    
    <body>
    	<p>Hello, <a href="123">World!</a></p>
    </body>
    
    </html>
    And now, with FF's DOM Inspector, you can see how the generated code ends up:
    Code:
    <html lang="en-us"><head><title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
    
    <script type="text/javascript">document.write('<!-- <textarea rows="0" cols="0" style="width: 0px; height: 0px; display: none;">');</script><!-- <textarea rows="0" cols="0" style="width: 0px; height: 0px; display: none;">
    <base href="http://www.w3.org/">
    <!-- kill --><!--></head><body>--&gt;<!-- kill -->
    
    	<p>Hello, <a href="123">World!</a></p>
    
    </body></html>
    Part of the problem, I'm sure, is that the HTML comment ends at the two dashes just before the first "kill." Everything after the first "kill" then becomes a convoluted syntax error. There is no defined way for the browser to deal with such wrong code, so the results are unpredictable.
    __________________
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";
    Reply With Quote
      #3  
    Old 05-21-2007, 01:34 AM
    robindean robindean is offline
    Registered User
     
    Join Date: Dec 2005
    Posts: 456
    I see, so we've got trouble as a result of the javascripts end result. The puzzle, once again, grows.
    Reply With Quote
      #4  
    Old 05-21-2007, 02:13 AM
    Fang's Avatar
    Fang Fang is offline
    Resistance is futile
     
    Join Date: Apr 2003
    Location: Netherlands
    Posts: 18,435
    Code:
    function removeComments() {
    var head=document.getElementsByTagName('head')[0];
    var children=head.childNodes;
    for(var i=0; i<children.length; i++) {
        if(children[i].nodeType==8) {head.removeChild(children[i]);}
        }
    }
    If there are multiple comments in the head, you may need to loop through the nodes in reverse order as the node map is immediately updated after a node is removed.
    __________________
    At least 98% of internet users' DNA is identical to that of chimpanzees

    Last edited by Fang; 05-21-2007 at 02:18 AM.
    Reply With Quote
      #5  
    Old 05-21-2007, 02:19 AM
    robindean robindean is offline
    Registered User
     
    Join Date: Dec 2005
    Posts: 456
    Thanks Fang!

    However, I managed to devise a solution to my alternate thread which involved a different outcome.

    Regardless, this answer strongly applies others who might be interested in doing this.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:28 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.