Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > cloneNode() not getting script tag content

Reply
Thread Tools

cloneNode() not getting script tag content

 
 
chippy
Guest
Posts: n/a
 
      01-03-2006
I am finding that when I use the cloneNode method to copy an HTML
element that contains a <script> tag, the contents of the <script> tag,
(ie. the javascript) are removed.

If I do this:
var form1 = document.getElementById(sID).firstChild.cloneNode( true);
alert(form1.outerHTML);

I can see the empty <script> tags. I am wondering if there is a
reasonable workaround for this, because I need the script tag with its
javascript content.

Thanks
Chip

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-03-2006


chippy wrote:

> I am finding that when I use the cloneNode method to copy an HTML
> element that contains a <script> tag, the contents of the <script> tag,
> (ie. the javascript) are removed.
>
> If I do this:
> var form1 = document.getElementById(sID).firstChild.cloneNode( true);
> alert(form1.outerHTML);
>
> I can see the empty <script> tags.


Seems to be an IE quirk, happens with both JavaScript and VBScript
inline code here for me with IE 6 on Windows XP SP 2.
Mozilla seems to do fine (well it does not have outerHTML support at all
but if I use innerHTML instead I can see the inline script code of
cloned script elements).
Opera 8 looses the inline script code (at least when looking at
outerHTML), Opera 9 Preview has it.

Why do you think you need to clone script code? Do you want to move it
into another window or frame? It does not seem to do much good to insert
cloned script functions into the same document.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
chippy
Guest
Posts: n/a
 
      01-04-2006
Martin,
Thank you for your reply.
I'll give some background on what I am doing. I have an HTML form that
is used for filling out text messages. (This form is created
dynamically using an XML Schema and XSLT.) Certain sections of the
message that the user is creating with this form may be repeated, so I
have created a button that runs a javascript function that copies the
portion of the form that may be repeated. The portion of the form that
can be repeated is surrounded by a form tag with a unique id that I
pass into the function. So I can do this:

var form1 = document.getElementById(sID).firstChild.cloneNode( true)

where sID is the id that is passed into the function and I get all of
the elements under the form tag.

With the script tags I am filling out a javascript associative array:

<script language="javascript" type="text/javascript">
fieldData["IDAEA1VIDAFA1V4"]
= {
DivID: "sic_code_4sic_code_or_filing_number",
ID: "IDATE0VIDAUE0V4",
Size: "3",
Type: "xsd:string",
Enum: "false",
minLength: "",
maxLength: "",
Length: "3",
minInclusive: "",
maxInclusive: "",
Pattern: "[A-Z0-9]{3}",
Enumeration: ""};
</script>


With the sID I am also able to reference the appropriate index of the
array. The array contains information that I use for validating the
input. Once I have a copy of the element, I can use the RegEx replace
to make all of the id's and the javascript (the array) unique.

I guess the bottom line is that I need some way to copy the elements I
need without doing it by reference (because then when I make changes to
the copied element the changes also go into the element I copied from).


I have tried to use a copy function like this:

function copy_obj(o) {
var c = new Object();

for (var e in o) {
c[e] = o[e];
}
return c;
}

but it appears that I am not getting an actual DOM object because it
doesn't recognize certain methods.

Anyhow, I think I have rambled on enough. I hope this gives you a
clear idea of what's happening, I appreciate any suggestions you might
have.

Many thanks.
Chip

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      01-04-2006


chippy wrote:


> With the script tags I am filling out a javascript associative array:
>
> <script language="javascript" type="text/javascript">
> fieldData["IDAEA1VIDAFA1V4"]
> = {
> DivID: "sic_code_4sic_code_or_filing_number",
> ID: "IDATE0VIDAUE0V4",
> Size: "3",
> Type: "xsd:string",
> Enum: "false",
> minLength: "",
> maxLength: "",
> Length: "3",
> minInclusive: "",
> maxInclusive: "",
> Pattern: "[A-Z0-9]{3}",
> Enumeration: ""};
> </script>
>
>
> With the sID I am also able to reference the appropriate index of the
> array. The array contains information that I use for validating the
> input. Once I have a copy of the element, I can use the RegEx replace
> to make all of the id's and the javascript (the array) unique.


But if that array (that looks like a JavaScript object to me) is a
global variable then simply manipulate it with your code, don't try to
rely on DOM node cloning to manipulate/copy JavaScript data structures
you control yourself.

> I guess the bottom line is that I need some way to copy the elements I
> need without doing it by reference (because then when I make changes to
> the copied element the changes also go into the element I copied from).
>
>
> I have tried to use a copy function like this:
>
> function copy_obj(o) {
> var c = new Object();
>
> for (var e in o) {
> c[e] = o[e];
> }
> return c;
> }


But you can do e.g.

var fieldData = {};
fieldData["IDAEA1VIDAFA1V4"]
= {
DivID: "sic_code_4sic_code_or_filing_number",
ID: "IDATE0VIDAUE0V4",
Size: "3",
Type: "xsd:string",
Enum: "false",
minLength: "",
maxLength: "",
Length: "3",
minInclusive: "",
maxInclusive: "",
Pattern: "[A-Z0-9]{3}",
Enumeration: ""};

function copy_obj(o) {
var c = new Object();

for (var e in o) {
c[e] = o[e];
}
return c;
}

fieldData['newId'] = copy_obj(fieldData["IDAEA1VIDAFA1V4"]);
fieldData['newId'].ID = 'newId';

As said, I would not mix your script data structure manipulation with
DOM manipulation. If you simply keep track of any stuff in fieldData
then your script can always add properties there and manipulate
subproperties without any need that the browser supports cloning inline
script elements and executing the cloned code when the cloned script
element is being inserted into the document. That approach is a rather
fragile.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
chippy
Guest
Posts: n/a
 
      01-04-2006
I hear you-- scrap the RegEx replace on the script element, and just
use the existing fieldData array (object)-- this makes too much sense,
I'll give it a whirl. I have definitely been doing it the hard way.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: Only Content controls are allowed directly in a content page that contains Content controls. hazz ASP .Net 6 06-09-2010 01:54 PM
Repeater: condition of a field being blank or not? to Tag or not to Tag HockeyFan ASP .Net 2 12-07-2006 03:53 PM
Select text within a div tag by clicking on content of div tag or a button? visu Javascript 4 11-22-2006 06:25 AM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
Select text within a div tag by clicking on content of div tag? M Wells Javascript 0 10-06-2004 11:04 AM



Advertisments