4 ms·
This solution fails when you need to insert the script more than once in a page. Instead, browsers really should provide scripts a means to retrieve the script
by enomar 16y ago
This solution fails when you need to insert the script more than once in a page. Instead, browsers really should provide scripts a means to retrieve the script element that they belong to.
- immad 16y agoYou can use a static counter to get around that
- enomar 16y agoSorry, can you explain what you mean?
- sesqu 16y agoA comment on the article pointed out that since javascript execution is blocking, you can do that for synchronous javascript: scripts=document.elements.getElementByTagName('script') latest=scripts[scripts.length-1]
- jauco 16y agoI wrote a small jQuery plugin around that idea ( at https://gist.github.com/435668/85466138a24814baef3ec6a73b7cc1f9187278f4 https://gist.github.com/435668/85466138a24814baef3ec6a73b7cc... ) This approach allows you to specify parameters as JSON. So data of arbitrary length can be passed without having to resort to some global variable. It solves a problem I had where I needed to pass some template engine generated content into an otherwise static script. Thanks for the tip :)
- enomar 16y agoRight, but then you have to require that your script be loaded synchronously. And this is really just a hack to get around the fact that browsers don't provide a proper API.