Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
rvagg opened this issue Sep 20, 2012 · 9 comments
Closed

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

rvagg opened this issue Sep 20, 2012 · 9 comments
Assignees

Comments

@rvagg
Copy link
Collaborator

rvagg commented Sep 20, 2012

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
Copy link
Contributor

aawwawa commented Nov 29, 2012

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
Copy link
Contributor

aawwawa commented Nov 30, 2012

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

@getify
Copy link

getify commented Nov 30, 2012

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
Copy link
Contributor

aawwawa commented Nov 30, 2012

@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
Copy link

getify commented Nov 30, 2012

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
Copy link
Contributor

aawwawa commented Nov 30, 2012

[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
Copy link
Owner

ded commented Nov 30, 2012

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...

@ghost ghost assigned ded Nov 30, 2012
ded added a commit that referenced this issue Nov 30, 2012
@ded
Copy link
Owner

ded commented Nov 30, 2012

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)

@ded ded closed this as completed Nov 30, 2012
@aawwawa
Copy link
Contributor

aawwawa commented Dec 1, 2012

Woah, cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants