Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #13721

Re: Deleting <script> elements

From Dr J R Stockton <reply1218@merlyn.demon.co.uk.not.invalid>
Newsgroups comp.lang.javascript
Subject Re: Deleting <script> elements
Date 2012-04-30 19:28 +0100
Organization Home
Message-ID <VOsK6lFWntnPFwNk@invalid.uk.co.demon.merlyn.invalid> (permalink)
References <timstreater-FB7160.19341329042012@news.individual.net>

Show all headers | View raw


In comp.lang.javascript message <timstreater-FB7160.19341329042012@news.
individual.net>, Sun, 29 Apr 2012 19:34:13, Tim Streater
<timstreater@greenbee.net> posted:

>Apart from the obvious potential loss of functionality, is there any
>reason I shouldn't delete all <script> elements from a page being
>displayed in an iframe by doing something along these lines:
>
>ptr   = window.frames["Frame1"].document;
>myarr = ptr.getElementsByTagName ('script');
>
>for  (i=0; i<myarr.length; i++)
>    {
>    ptr.removeChild (myarr[i]);
>    }
>
>I'd be planning to do this immediately after doing ptr.write
>(html_string), so any scripts in html_string wouldn't have had a chance
>to run, anyway.


If MyArr is not a plain array but a more dynamic thing (I forget the
proper term), then removing the first element could cause all the later
ones to renumber, and you would fall off the end when you thought you
were only half-way.

I'd use a while loop counting down to zero, and remove from the end
rather than the beginning, by habit.  That is a harmless change, I
think; it may also be necessary, and is probably a good habit.

It's not clear to me that you want to remove a *child*, though.  An
unsubtle alternative would be to loop through the scripts setting their
innerHTMLs to "", if they have such; or similar.

Test in all possible browsers.

-- 
 (c) John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v6.05   MIME.
   Web  <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
 Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
 Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Deleting <script> elements Tim Streater <timstreater@greenbee.net> - 2012-04-29 19:34 +0100
  Re: Deleting <script> elements "Evertjan." <exjxw.hannivoort@interxnl.net> - 2012-04-29 19:07 +0000
    Re: Deleting <script> elements Tim Streater <timstreater@greenbee.net> - 2012-04-29 20:53 +0100
  Re: Deleting <script> elements Martin Honnen <mahotrash@yahoo.de> - 2012-04-30 11:31 +0200
    Re: Deleting <script> elements Martin Honnen <mahotrash@yahoo.de> - 2012-04-30 11:33 +0200
    Re: Deleting <script> elements Tim Streater <timstreater@greenbee.net> - 2012-04-30 12:33 +0100
      Re: Deleting <script> elements Martin Honnen <mahotrash@yahoo.de> - 2012-04-30 13:55 +0200
        Re: Deleting <script> elements Tim Streater <timstreater@greenbee.net> - 2012-04-30 13:11 +0100
        Re: Deleting <script> elements Stefan Weiss <krewecherl@gmail.com> - 2012-04-30 14:47 +0200
          Re: Deleting <script> elements Martin Honnen <mahotrash@yahoo.de> - 2012-04-30 16:25 +0200
  Re: Deleting <script> elements Denis McMahon <denismfmcmahon@gmail.com> - 2012-04-30 21:38 +0000
    Re: Deleting <script> elements Tim Streater <timstreater@greenbee.net> - 2012-04-30 22:53 +0100
    Re: Deleting <script> elements Dr J R Stockton <reply1218@merlyn.demon.co.uk.not.invalid> - 2012-05-01 19:57 +0100
  Re: Deleting <script> elements Dr J R Stockton <reply1218@merlyn.demon.co.uk.not.invalid> - 2012-04-30 19:28 +0100

csiph-web