ded / bonzo

library agnostic, extensible DOM utility
Other
1.32k stars 137 forks source link

create('<script>') doesn't create a working script element #96

Closed rvagg closed 11 years ago

rvagg commented 12 years ago

Trail starts here: https://twitter.com/getify/status/248137610113986560

I gather that while we can create script elements they don't actually work.

/cc @getify

aawwawa commented 11 years ago

It occurred to me that since bonzo.create temporarily adds elements to the dom in order to create them, the scripts would download and execute regardless of whether the user is ready for them to or not... Even if we get this working, scripts should still only execute if they're appended to the DOM, IMO.

aawwawa commented 11 years ago

Or wait... maybe not, since it doesn't get added to the document's head or body objects themselves. My bad, I think... :frowning:

getify commented 11 years ago

If you're suggesting that they're temporarily added to a DOMFragment, then it's fine and safe.

But if you're suggesting they're added to the real DOM, it doesn't matter where a script element is added, the behavior is the same. When it's added, it's made subject to execution. In all versions of IE, the browser will start fetching the script as soon as you set src property, but it will still not execute it until after you've added it to the DOM.

However, some people actually utilize the behavior that IE prefetches scripts into the cache like that, so that's not necessarily a feature to code around or avoid. But certainly you don't want scripts to execute too early because you've inserted them into the real DOM "temporarily".

aawwawa commented 11 years ago

@getify yeah, I later realized that that was indeed what I was suggesting, so there's no problems... (Although, in any case, the bug isn't resolved yet, so it would still be at least temporarily moot.)

As far as IE goes, do you happen to know if it also prefetches scripts with types set to bogus, non-text/javascript values?

getify commented 11 years ago

IE <= 9 (dunno about 10, honestly) and older webkit (aka, webkit pre Mar 2011) fetch those types of scripts. The spec now specifically forbids such fetching, so any new and spec-conforming browser will not.

aawwawa commented 11 years ago

[Sorry, I think I misread what you wrote above and you actually answered that.]

Also, I wonder why the spec would forbid that... seems useful to me. Do you have any mailing list links or anything about the topic? I'm interested to know more, but hate to keep cluttering the bonzo tracker.

Thanks, BTW.

ded commented 11 years ago

nevertheless, the way to fix this is to detect that a script tag has in fact been included, and create one from scratch. perhaps i'll give this one a go...

ded commented 11 years ago

i've added a minimum viable solution for folks that require this functionality. it works as such for now

bonzo(bonzo.create('<script src="hello.js">')).appendTo(document.body)

note that it should not execute until it's appended to the live DOM. ultimately it work when the normalize stuff is in... (or with Ender now)

bonzo('<script src="hello.js">').appendTo(document.body)
aawwawa commented 11 years ago

Woah, cool!