[whatwg] setting .src of a SCRIPT element

Jonas Sicking jonas at sicking.cc
Wed May 30 18:27:13 PDT 2007


Hallvord R M Steen wrote:
> Hi,
> if you set the src property of a SCRIPT element in the DOM, IE will
> load the new script and run it. Firefox doesn't seem to do anything
> (perhaps a more seasoned bugzilla searcher can tell me if it is
> considered a known bug?).

It's by design (see below)

> I think Opera 8 does what IE does, Opera 9 is buggy.
> 
> I think IE's behaviour is pretty useful and I'd like the spec to make
> this standards-compliant. It is a common technique to create SCRIPT
> elements dynamically to load data (particularly because this gets
> around cross-domain limitations). Firefox's implementation means one
> has to create a new SCRIPT element each time, keep track of them, and
> remove them from the document again, whereas with IE's implementation
> you can have one "data loader" SCRIPT element and set its .src
> repeatedly.

The reason I designed it this way was that it felt like the least
illogical behavior. In general a document behaves according to its
current DOM. I.e. it doesn't matter what the DOM looked like before, or
how it got to be in the current state, it only matters what's in the DOM
now.

For <style> elements this work great. Whenever the contents of a <style>
is changed the UA can drop the current style rules associated with the
element, reparse or reload the new stylesheet, and apply the new style
rules to the document. (There was a bug in Firefox up to version 2,
where certain DOM mutations inside the <style> weren't detected, but
that has been fixed in Firefox 3).

For <script> things are a lot worse. If the contents of a <script>
element is changed it is impossible to 'drop' the script that was there
before. Once the contents of a <script> has executed, it can never be
unexecuted. And since we can't undo what the <script> has already done, 
it feels weird to redo the new thing that you're asking it to do.

Another thing that would be weird would be inline scripts. How would the 
following behave:
s = document.createElement('script');
document.head.appendChild(s);
for (i = 0; i < 10; i++) {
   s.textContent += "a" + i + " += 5;";
}
Would you reexecute the entire script every time data was appended to 
the script? Would you try to just execute the new parts? Would you do 
nothing? IE gets around this problem by not supporting dynamically 
created inline scripts at all, which I think is a really bad solution.

So I opted for 'killing' script elements once they have executed, they 
become in effect dead elements. This felt simple and consistent.

I'm not sure what you mean when you say you need to "keep track of them, 
and remove them from the document again". All you need to do every time 
you want to execute a script is to insert a new DOM element in the head 
of your page. It's not going to be a problem with having too many 
<script> elements in the document unless you start executing millions of 
scripts, at which point you'll have bigger performance issues.

/ Jonas



More information about the whatwg mailing list