Connecting Tech Pros Worldwide Forums | Help | Site Map

removing script tag

Asad Khan
Guest
 
Posts: n/a
#1: Jul 23 '05

I have an HTML file that has a call to a Javascript function in it as
follows:

<!-- bunch of stuff -->
<script type="text/javascript">doXMLFromString()</script>
<!-- bunch of stuff -->

Now I make a copy of this HTML file by creating a new window and writing

var body = document.body.innerHTML;
printWin.document.write (body);

But this copies the script tag above as well, and then tries to call
that JS function (doXMLFromString). I don't want it to do that.

In other words I dont want that javascript statement to be executed. I
tried removing it from the document (using removechild) however it would
still get called.

How can I stop this?


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Klaus Johannes Rusch
Guest
 
Posts: n/a
#2: Jul 23 '05

re: removing script tag


Asad Khan wrote:
[color=blue]
> In other words I dont want that javascript statement to be executed. I
> tried removing it from the document (using removechild) however it would
> still get called.[/color]

You need to remove the script element before writing out the new window,
that is

var body = document.body.innerHTML;
// remove script element here
printWin.document.write(body);

Alternatively you could set a window property and query that property to
suppress execution of the code in secondary windows, for example

printWin.isPrintWindow = true;

<script type="text/javascript">if(typeof(window.myProperty) ==
'undefined' || !window.isPrintWindow) doXMLFromString()</script>

or control the execution of the script based on the page address.

--
Klaus Johannes Rusch
KlausRusch@atmedia.net
http://www.atmedia.net/KlausRusch/
Randy Webb
Guest
 
Posts: n/a
#3: Jul 23 '05

re: removing script tag


Asad Khan wrote:
[color=blue]
> I have an HTML file that has a call to a Javascript function in it as
> follows:
>
> <!-- bunch of stuff -->
> <script type="text/javascript">doXMLFromString()</script>
> <!-- bunch of stuff -->
>
> Now I make a copy of this HTML file by creating a new window and writing
>
> var body = document.body.innerHTML;[/color]

body isn't a very good variable name. Does the second body refer to the
body of the page, or, to the variable body you just created?


var newBody = document.body.innerHTML;
var newBody = newBody.replace('offending script statement here','');

[color=blue]
> printWin.document.write (body);[/color]

printWin.document.write(newBody);
[color=blue]
> How can I stop this?[/color]

Simply replace the strings in the newBody that you don't want duplicated.


--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/
Closed Thread