W3C home > Mailing lists > Public > whatwg@whatwg.org > March 2009

[whatwg] Link.onload

From: Jonas Sicking <jonas@sicking.cc>
Date: Sun, 15 Mar 2009 01:07:47 -0700
Message-ID: <63df84f0903150107n2209165kece5a8a03c5fdc28@mail.gmail.com>
On Sat, Mar 14, 2009 at 10:21 PM, Garrett Smith <dhtmlkitchen at gmail.com> wrote:
> On Sat, Mar 14, 2009 at 6:54 PM, Jonas Sicking <jonas at sicking.cc> wrote:
>> On Sat, Mar 14, 2009 at 1:48 PM, Greg Houston <gregory.houston at gmail.com> wrote:
>
> [...]
>
>>
>> Garrett: Whatever we decide when it comes to the defer attribute, it
>> is always useful to have scripting APIs as well. There is just no way
>> that you can cover all use cases declaratively, so it's useful to be
>> able to fall back to using scripting for cases not covered.
>>
>
> What other cases do you have?

A web application, such as GMail, wanting to show a dialog box to the
user asking the user to enter some information. In order to do this it
needs to first load a stylesheet to properly style the dialog box. The
application would do this by inserting a <link rel="stylesheet"
href="dialog.css"> into the head of the document. It would then want
to know when the stylesheet has loaded so that it can display the
dialog box.

There's a few reasons depends is not a good solution for this scenario.

First off, in order to use it the page would have had to not only
insert the <link> element, but also ad a <script
depends="dialogStylesheetId">displayDialog()</script>.

Second, it gets even worse if a specific dialog is to be displayed,
since then the script has to not just generate the above script
element, but also dynamically generate the javascript code inside it,
for example <script depends="dialogStylesheetId">displayDialog("Dialog
Title")</script>. This is particularly bad if you'd want to pass a
reference to an object to the displayDialog function. The only way I
can think of for doing that would be using global variables, and
serializing the variable name of the global variable into the script.
This would be terrible software design.

Third, if something like Content Security Policy [1] gets adopted in
browsers it would be impossible to use an inline script. You would
instead have to host a separate file that contains the call to
displayDialog() call. This would get even worse when combined with the
ability to show a specific dialog as described above.

It seems much simpler to be able to do:

link = document.createElement('link');
link.rel = "stylesheet";
link.href = "dialog.css";
link.onload = function() {
  displayDialog("Dialog Title", someObject);
}
document.getElementsByTagName('head')[0].appendChild(link);

/ Jonas

[1] http://people.mozilla.org/~bsterne/content-security-policy/
Received on Sunday, 15 March 2009 01:07:47 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Wednesday, 30 January 2013 18:47:49 GMT