Closed
Bug 131613
Opened 23 years ago
Closed 22 years ago
Function defined in DOM-appended <SCRIPT> elt not visible
Categories
(Core :: DOM: Core & HTML, defect)
Tracking
()
RESOLVED
WONTFIX
People
(Reporter: bmf1972, Unassigned)
Details
Attachments
(3 files)
The following code doesn't work under Mozilla {Build ID:2002031104}:
<HTML>
<HEAD>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<SCRIPT>
var
g_o; // A globally available object
// Define a class
function DynObj() {
// with one (static or early defined) method
this.StaticDoIt = StaticDoIt;
}
// Define the static method
function StaticDoIt() {
alert( 'STATIC: Done it!' );
}
// Initiate the global object
function Init() {
g_o = new DynObj();
}
// Create a function dynamically
function CreateDynMethod() {
var
oText = document.createTextNode(''),
oScript = document.createElement( 'SCRIPT' ),
oHead = document.getElementsByTagName( 'HEAD' )[0];
oHead.appendChild( oScript );
oScript.appendChild( oText );
// any function could be created here
oText.nodeValue = "function DynamicDoIt() { alert( 'DYNAMIC: Done it!' ); }";
}
// Bind the dynamic created function to the passed in object
function BindDynMethod( o ) {
o.DynamicDoIt = DynamicDoIt;
}
</SCRIPT>
</HEAD>
<body onload="Init()">
<table border=1 bgcolor=LightCyan>
<tr bgcolor=SpringGreen>
<td align=right>This allways works</td>
<td align=center><input type=button value="Static - Do It"
onclick="g_o.StaticDoIt()"></td>
</tr>
<tr bgcolor=Crimson>
<td align=right>This will work only after (1)CREATING and (2)BINDING the dynamic
method</td>
<td align=center><input type=button value="Dynamic - Do It"
onclick="g_o.DynamicDoIt()"></td>
</tr>
<tr bgcolor=Lavender>
<td colspan=2>
<input type=button value="(1) Create Dynamic Method" onclick="CreateDynMethod()">
<input type=button value="(2) Bind Dynamic Method" onclick="BindDynMethod(g_o)">
</td>
</tr>
</table>
</body>
</HTML>
Under Internet Explorer v5.0/5.5/6.0 the CreateDynMethod() function is slightly
different (because of DOM differences):
// Create a function dynamically
function CreateDynMethod() {
var
//oText = document.createTextNode(''),
oScript = document.createElement( 'SCRIPT' ),
oHead = document.getElementsByTagName( 'HEAD' )[0];
oHead.appendChild( oScript );
//oScript.appendChild( oText );
// any function could be created here
//oText.nodeValue = "function DynamicDoIt() { alert( 'DYNAMIC: Done it!' ); }";
oScript.text = "function DynamicDoIt() { alert( 'DYNAMIC: Done it!' ); }";
}
*****************
It seems that the JavaScript engine in Mozilla doesn't parse <SCRIPT> blocks
created during run-time (dynamically); it does parse dynamically created scripts
in InternetExplorer!
Comment 2•23 years ago
|
||
Comment 3•23 years ago
|
||
Comment 4•23 years ago
|
||
Confirming reported behavior using Mozilla trunk binaries 20020314xx
on WinNT, Linux. Load the first testcase above and click on button (1).
When you click on button (2) "Bind Dynamic Method", you get this error
in the JS Console:
Error: DynamicDoIt is not defined
Source File: http://bugzilla.mozilla.org/attachment.cgi?id=74660&action=view
Line: 57
Note the DynamicDoIt() function was supposed to have been created
when we clicked on button (1), "Create Dynamic Method".
The IE version works in IE. Since the construct used here: creating
a <SCRIPT> element dynamically, is a DOM issue, reasssigning to DOM.
cc'ing Boris, jkeiser -
Assignee: rogerl → jst
Status: UNCONFIRMED → NEW
Component: JavaScript Engine → DOM Level 0
Ever confirmed: true
OS: Windows 2000 → All
QA Contact: pschwartau → desale
Updated•23 years ago
|
Summary: Dynamic script → Function defined in DOM-appended <SCRIPT> elt not visible
Comment 5•23 years ago
|
||
What happens if you set oText.nodeValue before appending the whole thing to <head>?
Comment 6•23 years ago
|
||
Comment 7•23 years ago
|
||
Boris' suggestion works. I changed the order to this:
function CreateDynMethod()
{
var oText = document.createTextNode('');
var oScript = document.createElement('SCRIPT');
var oHead = document.getElementsByTagName('HEAD')[0];
// any function could be created here
oText.nodeValue = "function DynamicDoIt() {alert('DYNAMIC: Done it!');}";
oScript.appendChild(oText);
oHead.appendChild(oScript);
}
Try the third attachment; it works in Mozilla.
Comment 8•23 years ago
|
||
SUMMARY. THIS ORDER OF OPERATIONS WORKS:
function CreateDynMethod()
{
var oText = document.createTextNode('');
var oScript = document.createElement('SCRIPT');
var oHead = document.getElementsByTagName('HEAD')[0];
oText.nodeValue = "function DynamicDoIt() {alert('DYNAMIC: Done it!');}";
oScript.appendChild(oText);
oHead.appendChild(oScript);
}
THIS ORDER DOESN'T WORK. IS IT SUPPOSED TO?
function CreateDynMethod()
{
var oText = document.createTextNode('');
var oScript = document.createElement('SCRIPT');
var oHead = document.getElementsByTagName('HEAD')[0];
oHead.appendChild(oScript);
oScript.appendChild(oText);
oText.nodeValue = "function DynamicDoIt() {alert('DYNAMIC: Done it!');}";
}
Comment 9•23 years ago
|
||
What about the following:
function CreateDynMethod()
{
var oText = document.createTextNode('');
var oScript = document.createElement('SCRIPT');
var oHead = document.getElementsByTagName('HEAD')[0];
oHead.appendChild(oScript);
oText.nodeValue = "function DynamicDoIt() {alert('DYNAMIC: Done it!');}";
oScript.appendChild(oText);
}
I suspect that also does not work... but I feel like it should.
Order #2 is a little weird. You're changing the text node, but how is the
<script> supposed to know that the text node changed? It _could_ be made to
work, but I'm not sure whether we want it to....
Comment 10•23 years ago
|
||
Confirming Boris' suspicion: the order of operation in Comment #9
does NOT work - we get the same error as in Comment #4.
Comment 11•23 years ago
|
||
The "doesn't work" case is like dynamically modifying the content of the script.
What should we do? For example let's say the user dynamically removes a var
declaration. Should we reevaluate the script and throw an error if needed?
It's one thing to support the insertion of a <script> element (which we do,
though see bug 26790). But supporting the dynamic modification of the _text
content_ of a script is another. Does the js engine even support it?
Comment 12•23 years ago
|
||
This will perhaps be fixed by the patch in bug 26790.
Comment 13•22 years ago
|
||
I agree with bz here. I think modifying/removing a previous script is very hard
to do, if not impossible. I don't think we should go there unless someone knows
of an easy way to do it. But the example Boris mentions will work with the patch
in 26790.
Comment 14•22 years ago
|
||
Consider this:
I have an empty DIV in the document. At some point, I dynamically create an
IFRAME in which I load a different document. Upon loading, this document (the
one in the IFRAME) identifies the DIV in the parent document and sets its
innerHTML property to some html which also includes a linked script (with src=).
Then the IFRAME is destroyed.
The html is rendered ok in the DIV, but the script it references does not get
evaluated. Functions or variables defined in it are not accessible. However, the
JavaScript Debugger will see the script and its contents.
This behavior was introduced in 1.0RC3. Everything worked fine in 0.9.8.
Comment 15•22 years ago
|
||
That's not this bug. Please file it as a separate bug (preferably with a
testcase).
Comment 16•22 years ago
|
||
Thanks, Boris. I filed it as 147581.
Comment 17•22 years ago
|
||
restoring CC's
Comment 19•22 years ago
|
||
sicking, this seems to be up your alley (as in, you've wontfixed some bugs of
this general character recently.... ;) )
Yes, this is indeed a WONTFIX.
Mozilla marks a script-element as evaluated once it is in the tree and either
has a src-attribute or it has children. And once a script-element has been
evaluated it will never be reevaluated. This was implemented in bug 26790.
The fact that the textnode is empty doesn't make a difference to mozilla. We
could make mozilla take this into account, but I don't really see a reason to do
this since this doesn't work in any other browser anyway.
So to sum up; To do dynamic creation you have to set the value of the textnode
before you append it. Otherwise it will be considered dynamic modification of a
script, which is not supported.
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → WONTFIX
You need to log in
before you can comment on or make changes to this bug.
Description
•