|
|
adammcarth |
prevent stylesheet from adding twice
…
- at the moment if loadCSS() is called twice (this situation is perfectly feasible), the stylesheet will also be added twice. This fix removes the existing stylesheet (by checking the href parameter against the one passed to the loadCSS() function) before the new one is added to the DOM. |
a554ace
|
|
|
|
adammcarth |
update minified version for new patch
|
4d3db6b
|
|
|
|
adammcarth |
fix the formatting
…
2 spaces out please |
ed9983a
|
|
|
|
adammcarth |
fix formatting v2
|
8efb010
|
Hey, thanks. Can you explain the issue this change addresses? I'm not sure why you're trying to load the same file twice.
Sure
So let's say you have a form that submits though ajax and you want to load a new stylesheet asynchronously if the server hits back with a 200 OK (perhaps some css to format a success message). If the form is submitted twice or more (very feasible in some scenarios) - the stylesheet will be appended to the DOM more than once. Although this shouldn't make a difference to the rendering of the css, appending the same stylesheet to the document more than once seems very wrong.
So no, you'd be out of your mind to add the stylesheet twice like I did in my example above - I just wanted to show that it will be added again without removing the old one first.
The changes I made check each stylesheet's href="" attribute, and if it matches the one specified in the loadCSS() function - it will be removed before it is added again. I was doing some deeper thinking whilst running this morning, perhaps it should check that the media="" attribute is the same too before removing it?
Checking the media attribute would get very messy since ss.media = media || "all" was used in a setTimeout() function. You'd have to set something like data-real-media to access the real media value (instead of "only x"), but I don't want to overcomplicate things at this stage. If we were to do that I think it should be a separate PR.
How are we feeling @scottjehl?
Thanks for checking in, @adammcarth. We talked it over internally and we don’t feel that this addition meets a common enough use case to include in this simple script. That said, I’d encourage you to keep the fork open if you find it useful! Thanks again for the PR.
Why not just cancelling the second request/change?
Great point Benjamin. I think it would be better not to reload already loaded files, that would result in unnecessary HTTP requests.
@Benjamin-K @inta I thought for quite a long time about that. Reload the stylesheet or prevent it from adding again?
I settled on reloading it - what if the stylesheet was dynamically modified by an external process? The changes wouldn't be applied if we ignored the request. Whilst most adding it again would probably result in an unnecessary http request most of the time, you have to cater for all scenarios.
Yeah, I thought about that, but came to the conclusion that this is a rare use case. In production environments I do not change my stylesheets anyway, as they should be cached. I think this is out of the scope of this script, because you need to clear the cache either if you really want to force reload of a CSS file.
True. I'll probably update my branch with that functionality in that case - but I still don't think it will change Scott's mind.
prevent stylesheet from adding twice …
- at the moment if loadCSS() is called twice (this situation is perfectly feasible), the stylesheet will also be added twice. This fix removes the existing stylesheet (by checking the href parameter against the one passed to the loadCSS() function) before the new one is added to the DOM.
| @@ -1 +1 @@ | ||
| -function loadCSS(e,t,n){"use strict";var r=window.document.createElement("link");var i=t||window.document.getElementsByTagName("script")[0];r.rel="stylesheet";r.href=e;r.media="only x";i.parentNode.insertBefore(r,i);setTimeout(function(){r.media=n||"all"})} | ||
| +function loadCSS(e,t,n){"use strict";var r=window.document.getElementsByTagName("link");for(var i=0,s;s=r[i];i++){if(s.getAttribute("href")===e){s.remove()}}var s=window.document.createElement("link");var o=t||window.document.getElementsByTagName("script")[0];s.rel="stylesheet";s.href=e;s.media="only x";o.parentNode.insertBefore(s,o);setTimeout(function(){s.media=n||"all"})} |
The problem:
This fix removes the existing stylesheet (by checking the href parameter against the one passed to the
loadCSS()function) before the new one is added to the DOM.Adam :)