最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is the order of onload handler and src set important in a script element? - Stack Overflow

programmeradmin2浏览0评论

Coming from this answer that says:

You should set the src attribute after the onload event, f.ex:

el.onload = function() { //...
el.src = script;

You should also append the script to the DOM before attaching the onload event:

$body.append(el);
el.onload = function() { //...
el.src = script;

Remember that you need to check readystate for IE support. If you are using jQuery, you can also try the getScript() method: .getScript/

I am skeptical about believing that this would be the correct answer to the question.

So, is the order of setting onload handler and src so important? I think they are evaluated by browser instantly so I think that there is no difference between:

el.onload = function() { //...
el.src = script;

and

el.src = script;
el.onload = function() { //...

Am I right?

Coming from this answer that says:

You should set the src attribute after the onload event, f.ex:

el.onload = function() { //...
el.src = script;

You should also append the script to the DOM before attaching the onload event:

$body.append(el);
el.onload = function() { //...
el.src = script;

Remember that you need to check readystate for IE support. If you are using jQuery, you can also try the getScript() method: http://api.jquery./jQuery.getScript/

I am skeptical about believing that this would be the correct answer to the question.

So, is the order of setting onload handler and src so important? I think they are evaluated by browser instantly so I think that there is no difference between:

el.onload = function() { //...
el.src = script;

and

el.src = script;
el.onload = function() { //...

Am I right?

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Jan 19, 2014 at 8:08 Ionică BizăuIonică Bizău 114k94 gold badges310 silver badges487 bronze badges 6
  • 1 I dont think the order is important. Even if the image is loaded from cache instantly, the onload callback will only get queued, since js doesnt do multi threaded exec, right? – z33m Commented Jan 19, 2014 at 8:12
  • 1 I'd guess if the JS took a pee break, onload might be missed. – bjb568 Commented Jan 19, 2014 at 8:12
  • @z33m unfortunately not right... webkit browsers will make the onload event synchronously fires (it fires in the flow as soon as possible). I'm not sure it's a bug per specs (couldn't find it) but FF does queue the event handler, and if specs don't tell to do so, I'd think it's a bug in specs. But anyway, since this behavior exists in webkit, one should always set the src after the onload handler has been declared. – Kaiido Commented Jul 26, 2016 at 2:06
  • That precaution would only be true on img elements. script elements don't get pre-loaded like img elements do. – Bekim Bacaj Commented Jul 26, 2016 at 3:11
  • @BekimBacaj, unless you set their async attribute yes. Still true for other media elements (audio, video, object, iframe, embed, SVGImage ...). – Kaiido Commented Jul 26, 2016 at 5:45
 |  Show 1 more ment

2 Answers 2

Reset to default 3

Is 'el' already part of your live DOM? If so, when you change its onload event handler, it's contents will not be evaluated (because the load event already occurred).

el.onload = function() { //...

If el is not already added to the page, for instance if you're building a view, and will be injected into the page when everything is set, then yes it's load event will be fired when it's added to the page.

Be cautious about what might in the 'src' might depend on what happened in 'onload'.

@thinkbigthinksmall is right.

I would add that this behavior is webkit specific, Firefox will queue the event handler, so in this case it wouldn't matter.

I am not sure what the specs do say about it, but IMM FF behavior is the correct one.

Anyway, since webkit behaves like that, one should always set the src after the onload event has been declared, otherwise, cached media will fire the onload event before your script has been parsed.

One workaround though is to set your src again after the onload event has been declared :

<img src="someCachedMedia.ext" id="img"/>
<script>
  img.onload = doStuff;
  img.src = img.src; // force the onload to fire again
</script>

This way, you're sure the onload event will fire. Also, a single request should be made, since the first one will be cached.

发布评论

评论列表(0)

  1. 暂无评论